Makefile & precompiled header

General OpenMP discussion

Makefile & precompiled header

Postby alessandro » Thu Jun 20, 2013 10:25 pm

On the test for understand the performance benefit of a parallel applications using OpenMP I use the CImg library then I noticed that from the header CImg.h (2.2mb) came a precompiled header CImg.h.gch (36.5mb), exist some way for reuse it after the first compilation?
On this way I should save some time at compile time.

Code: Select all
CC = g++
DEBUG = -g
CFLAGS = -fopenmp
LDFLAGS = -lX11 -lpthread
PCH_FILENAME=CImg.h
PCH=$(PCH_FILENAME).gch

$(PCH): convert.cpp $(PCH_FILENAME)
   $(CC) -x c++-header $(PCH_FILENAME) convert.cpp $(LDFLAGS) -o grayscale
all: convert.cpp CImg.h
   $(CC) $(CFLAGS) CImg.h convert.cpp $(LDFLAGS) -o grayscale
   @echo "Done"
noomp: all # just for test the use of this make feautures
omp: convert.cpp CImg.h
   $(CC) $(CFLAGS) -c CImg.h convert.cpp $(LDFLAGS)
   @echo "Generate the file .o"
   $(CC) $(CFLAGS) -o grayscale convert.o $(LDFLAGS)
   @echo "Done"
debug: convert.cpp CImg.h
   $(CC) $(CFLAGS) $(DEBUG) CImg.h convert.cpp $(LDFLAGS) -o grayscale
profiling: convert.cpp CImg.h
   $(CC) $(CFLAGS) -pg CImg.h convert.cpp $(LDFLAGS) -o grayscale
clean:
   rm convert.o grayscale


If i use that code:
Code: Select all
PCH_FILENAME=CImg.h
PCH=$(PCH_FILENAME).gch

$(PCH): convert.cpp $(PCH_FILENAME)
   $(CC) -x c++-header $(PCH_FILENAME) convert.cpp $(LDFLAGS) -o grayscale

I obtain:
$ make
make: "CImg.h.gch" è aggiornato.
and don't do anything at all.
While using:
make omp
the CImg.h.gch is generated each time during the compilation.
alessandro
 
Posts: 26
Joined: Tue Jun 11, 2013 2:14 am

Re: Makefile & precompiled header

Postby MarkB » Fri Jun 21, 2013 2:07 am

Is compilation time really a problem for a small test code?


$ make
make: "CImg.h.gch" è aggiornato.
and don't do anything at all.


That's looks correct: if the .h file has not changed there is no need to rebuild the .h.gch


While using:
make omp
the CImg.h.gch is generated each time during the compilation.


I think that may be because you are explicitly specifying the .h file in the compile command.
MarkB
 
Posts: 481
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Makefile & precompiled header

Postby alessandro » Fri Jun 21, 2013 6:17 am

MarkB wrote:That's looks correct: if the .h file has not changed there is no need to rebuild the .h.gch

Yes but the issue is that don't compile the cpp file and so don't generate a new executable.
Also if i remove the precompiled header i obtain the follow error:
$ rm CImg.h.gch
$ make
Code: Select all
g++ -x c++-header CImg.h convert.cpp -lX11 -lpthread -o grayscale
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [CImg.h.gch] Errore 1


MarkB wrote:I think that may be because you are explicitly specifying the .h file in the compile command.

Using:
Code: Select all
omp: convert.cpp $(PCH)
   $(CC) $(CFLAGS) -c $(PCH) convert.cpp $(LDFLAGS)
   @echo "Generate the file .o"
   $(CC) $(CFLAGS) -o grayscale convert.o $(LDFLAGS)
   @echo "Done"

I obtain the same error as above.

Using:
Code: Select all
$(PCH): $(PCH_FILENAME) convert.cpp
   $(CC) -c $(PCH_FILENAME) -o $(PCH) -c convert.cpp $(LDFLAGS)

I obtain the same result.

MarkB wrote:Is compilation time really a problem for a small test code?

If i can rid the small stuff during the test, later I can focus on more important part also since I use obsolete computers, while make many change, the total sum of the time saved would not be so limited.
alessandro
 
Posts: 26
Joined: Tue Jun 11, 2013 2:14 am

Re: Makefile & precompiled header

Postby alessandro » Sat Jun 22, 2013 12:17 am

Follow this article I made some change:
Code: Select all
OBJS = convert.o
CC = g++
CXX = g++ -c
DEBUG = -g
CFLAGS = -fopenmp
LDFLAGS = -lX11 -lpthread
PCH_FILENAME=CImg.h
PCH=$(PCH_FILENAME).gch

define compile
$(CXX) -o $@ $<
endef

$(PCH): $(PCH_FILENAME)
   $(compile)
#$(PCH): $(PCH_FILENAME) convert.cpp
#   $(CC) -c $(PCH_FILENAME) -o $(PCH)
#   $(CC) $(CFLAGS) -c convert.cpp -o grayscale convert.o $(PCH) $(LDFLAGS)
#   $(CC) -x c++-header -o $(PCH) -c convert.cpp $(LDFLAGS)
omp: convert.cpp CImg.h
   $(CC) $(CFLAGS) -I. -include $(PCH_FILENAME) -c convert.cpp -fpch-preprocess $(LDFLAGS)
   #$(CC) $(CFLAGS) -c CImg.h convert.cpp $(LDFLAGS)
   @echo "Generate the file .o"
   $(CC) $(CFLAGS) -o grayscale convert.o $(LDFLAGS)
   @echo "Done"
all: convert.cpp CImg.h
   $(CC) $(CFLAGS) CImg.h convert.cpp $(LDFLAGS) -o grayscale
   @echo "Done"
noomp: all # just for test the use of this make feautures
debug: convert.cpp CImg.h
   $(CC) $(CFLAGS) $(DEBUG) CImg.h convert.cpp $(LDFLAGS) -o grayscale
profiling: convert.cpp CImg.h
   $(CC) $(CFLAGS) -pg CImg.h convert.cpp $(LDFLAGS) -o grayscale
clean:
   rm convert.o grayscale


Using this line of code:
$(CC) $(CFLAGS) -I. -include $(PCH_FILENAME) -c convert.cpp -fpch-preprocess $(LDFLAGS)

I can use the pre-compiled header (if is present) while I obtain no error (if isn't present) but doing:
make omp
if the pre-compiled header isn't present, it isn't generated.
Instead I must doing:
make
make omp

So now remain only one issue, how generated the pre-compiled header when isn't present automatically without jump from one command to another command.
For example if i use that form:
Code: Select all
omp: convert.cpp CImg.h
   $(CC) $(CFLAGS) -I. -include $(PCH_FILENAME) -c $(PCH_FILENAME) -fpch-preprocess -o $(PCH) $(LDFLAGS)
   $(CC) $(CFLAGS) -I. -include $(PCH_FILENAME) -c convert.cpp -fpch-preprocess $(LDFLAGS)
   #$(CC) $(CFLAGS) -c CImg.h convert.cpp $(LDFLAGS)
   @echo "Generate the file .o"
   $(CC) $(CFLAGS) -o grayscale convert.o $(LDFLAGS)
   @echo "Done"

The pre-compiled header is generated each time while I would like a way that it is generated only when isn't present or are present modification on the .h file.
alessandro
 
Posts: 26
Joined: Tue Jun 11, 2013 2:14 am

Re: Makefile & precompiled header

Postby alessandro » Sat Jun 22, 2013 11:35 am

Follow that howto:
Variables simplify makefiles

Code: Select all
OBJS = convert.o
CC = g++
CXX = g++ -c
CFLAGS = -fopenmp
LDFLAGS = -lX11 -lpthread
PCH_FILENAME=CImg.h
PCH=$(PCH_FILENAME).gch

define compile
$(CXX) -o $@ $<
endef

grayscale:$(OBJS) $(PCH)
   $(CC) $(CFLAGS) -o grayscale $(OBJS) $(LDFLAGS)
$(PCH): $(PCH_FILENAME)
   $(compile)   
convert.o: convert.cpp $(PCH_FILENAME)
   $(CC) $(CFLAGS) -I. -include $(PCH_FILENAME) -c convert.cpp -fpch-preprocess $(LDFLAGS)
debug: convert.cpp CImg.h
   $(CC) $(CFLAGS) $(DEBUG) CImg.h convert.cpp $(LDFLAGS) -o grayscale
profiling: convert.cpp CImg.h
   $(CC) $(CFLAGS) -pg CImg.h convert.cpp $(LDFLAGS) -o grayscale
clean:
   rm convert.o grayscale


I obtain the desired result but if I add the code:
Code: Select all
all: convert.cpp CImg.h
   $(CC)  CImg.h convert.cpp $(LDFLAGS) -o grayscale
   @echo "Done"

for test the performance benefit between the application with or without OpenMP is present another problem.
Essentially doing:
make all
I can force the compilation (and later I think I will optimize it for use the precompiled header) but doing:
make
or
make grayscale
I can't force the compilation for OpenMP for rebuild the executable.

And while I can force the rebuild of the executable doing:
make debug
or
make profiling
only the executable is update (but i must still test if the executable contain the proper informations from the options) and again if later I try doing:
make
or
make grayscale
don''t happen nothing.

I suppose a workaround could be use a different name for the executable with a minimum of three name, example:
grayscale for the omp
grayscale-dp for debug/profiling
grayscale-noomp for all
but i don't like much this trick. :|
alessandro
 
Posts: 26
Joined: Tue Jun 11, 2013 2:14 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 13 guests

cron