6.1 Sequential versions with 2 compilers¶
We will begin by illustrating that the same code file for vector addition can be compiled and run by different C compilers: gcc and pgcc. All of the examples in this chapter use the same functions for gathering command line arguments and for utility functions that initialize the vectors, print them, and check for correct results. These are in separate files in the code on the repository and are shown in code blocks below whose run button is disabled.
Note
The OpenACC compiler, pgcc, uses the .c suffix, because code files are treated as C code files that can be compiled with a C compiler. Though the pgcc compiler is creating CUDA code behind the scenes for versions that will run on the GPU, we now think of OpenACC code files as C code files with pragmas, much like OpenMP.
Command line argument handling¶
files: utils/getCommandLine.h, utils.getCommandLine.c
The following code block contains the functions for gathering the command line arguments for our vector addition program. It uses a C library function call getopt(), which enables us to use syntax like this when executing this from the command line on your own machine:
./vectorAdd -n 1024 -t 4
The getopt() function is used on line 19 below. If you have not used this before, you should be able to find tutorials on the web for it.
Helper functions used by each example¶
The following functions are used for each different main program function that you find here and in the sections following this one.
files: utils/helper_add.h
Sequential main program: gcc compiler¶
The main program below is compiled by including the above two code blocks. As you can see below the code, there is a place for you to change the command line arguments, as we have seen in other examples in this book and the PDC for Beginners book. There is also now a box where we expose the compiler arguments that in this case are sent to the gcc compiler. Run it first with all of these values given to you.
Notes:
Note that for this code, if the number of values in each array is less than 40, the arrays will be printed so that you can verify visually what is in them and that it is added them correctly. You can experiment with the ‘10’ n the command line arguments to illustrate this.
Since this is a sequential version, the option for number of threads is ignored.
The default compiler flags provided are -Ofast (capital O, not zero) for a fast code optimization level, and -lm for math library functions. To generate a compiler error you could try adding ,’-foo’ to the comma-separated list after ‘-lm’.
Sequential main program: pgcc compiler¶
The main program is compiled by including the two code blocks for command line arguments and helper functions. You can run this one to see how the new pgcc compiler runs the program and creates compiler output that is explained below.
Here is how to interpret the output from running this version:
The output from the program comes first. Note how it is the same as the default gcc version above.
After the program output, there is a line that looks like this: ===== STANDARD ERROR =====. What follows this is the output from the pgcc compiler. We indicated that we wanted this output by using the compiler flag ‘-Minfo=opt’. This pgcc compiler provides this option so that you can see the optimizations the compiler used as a result of including the -fast option. Look at the Wikipedia page for loop unrolling to see a discussion of this technique, which was the optimization used here.