C – FASRC DOCS https://docs.rc.fas.harvard.edu Wed, 08 Jan 2025 03:57:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://docs.rc.fas.harvard.edu/wp-content/uploads/2018/08/fasrc_64x64.png C – FASRC DOCS https://docs.rc.fas.harvard.edu 32 32 172380571 Cpp, C++ Programming Language https://docs.rc.fas.harvard.edu/kb/cpp-programming-language/ Tue, 30 Apr 2024 13:44:17 +0000 https://docs.rc.fas.harvard.edu/?post_type=epkb_post_type_1&p=26934 Description

C++ (C plus plus) is an object-oriented high-level programing language. C++ files typically have .cpp as the file extension. You can compile C++ codes with either GNU compilers (gcc) or Intel compilers (intel).

Best Practice

We recommend requesting an interactive job to compile a C++ program on a compute node (instead of a login node). The compilation could take up to few seconds to a minute and depending on the complexity of the code. Additionally, it is best to utilize the test partition to compile and test a program before executing its production run on the cluster as a batch-job.

It is best practice to compile a C++ code separately and then use the executable, generated during compilation, in the production run using the sbatch script. If possible, avoid including the compilation command in the sbatch script, which will recompile the program every time the job is submitted. If any changes are made to the source code, compile the source code separately, and then submit the production run as a batch-job.

Compilers

You can compile a C++ code using either a GNU or an Intel compiler.

GNU compiler

To use C++ with gcc on the FASRC clusters, load gcc compiler via our module system. For example, this command will load the latest gcc version:

module load gcc

If you need a specific version of R, you can search with the command

module spider gcc

To load a specific version

module load gcc/10.2.0-fasrc01

For more information on modules, see the Lmod Modules page.

To compile a code using a specific version of the GNU compiler and the O2 optimization flag, you can do the following:

module load gcc 
g++ -O2 -o sum.x sum.cpp

Intel compiler

To use C++ with Intel on the FASRC clusters, load intel compiler via our module system. For example, this command will load the latest intel version:

module load intel

If you need a specific version of R, you can search with the command

module spider intel

To load a specific version

module load intel/24.0.1-fasrc01

For more information on modules, see the Lmod Modules page.

Intel recommendations and notes

  • Intel released Intel OneAPI 23.2 with icpx, however, this version does not contain all the features, so we highly recommend using Intel 24 for icpx
  • Intel 17 is quite old. Avoid using it as it can have many incompatibilities with the current operating system
  • Intel has changed its compiler in the past few years and each module may need different flags. Below is a table of executables and possible flags
Intel module versionCommandAdditional flag
intel/17.0.4-fasrc01icpc-std=gnu++98
intel/23.0.0-fasrc01icpc
intel/23.2.0-fasrc01icpx
intel/24.0.1-fasrc01icpx

To compile using a specific version of the Intel compiler, execute:

module load intel/24.0.1-fasrc01
icpx -O2 -o sum.x sum.cpp

Examples

FASRC User Codes

]]>
26934
C Programming Language https://docs.rc.fas.harvard.edu/kb/c-programming-language/ Tue, 23 Apr 2024 10:24:01 +0000 https://docs.rc.fas.harvard.edu/?post_type=epkb_post_type_1&p=26991 Description

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. By design, C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to embedded systems. Wikipedia

Best Practice

We recommend jumping to a compute node for compiling a C program as the compilation could take up to few seconds to a minute depending on the complexity of the code. Additionally, it is best to utilize the test partition to compile and test a program before executing its production run on the cluster as a batch-job.

It is best practice to compile a C code separately and then use the executable, generated during compilation, in the production run using the sbatch script. If possible, avoid including the compilation command in the sbatch script, which will recompile the program every time the job is submitted. If any changes are made to the source code, compile the source code separately, and then submit the production run as a batch-job.

Compilers

You can compile a C code using either a GNU or an Intel compiler.

GNU gcc compiler

To get a list of currently available GNU compilers on the cluster, execute: module spider gcc

The default GNU compiler is typically the latest compiler version on the cluster and can be loaded using module load gcc

To compile a code using a specific version of the GNU compiler and the O2 optimization flag, you can do the following:

module load gcc/9.5.0-fasrc01
gcc -O2 -o sum.x sum.c

Intel icc compiler

To get a list of currently available Intel compilers on the cluster, execute: module spider intel

To compile using a specific version of the Intel compiler, execute:

module load intel/23.0.0-fasrc01
icc -O2 -o sum.x sum.c

Note: If loading an intel module version, refer to the following table for compiling your C code

Intel Comiler Version C Fortran C++
Below 24.0.0 icc ifortran icpc
24.0.0 and above icx ifx icpx

If you load an Intel compiler that is lower than version 24.0.0, you might get this remark

icc: remark #10441: The Intel(R) C++ Compiler Classic (ICC) is deprecated and will be removed from product release in the second half of 2023. The Intel(R) oneAPI DPC++/C++ Compiler (ICX) is the recommended compiler moving forward. Please transition to use this compiler. Use '-diag-disable=10441' to disable this message.

This is just a warning that implies that the user of icc will be deprecated in the second half of 2023. You can quiet this warning by compiling your code using icc in the following manner:

module load intel/23.0.0-fasrc01
icc -O2 -diag-disable=10441 -o sum.x sum.c

Examples

To get started with C on the Harvard University FAS cluster you can try the example shown on our User Codes repository.

Resources

To learn and practice more in C, see the following:

]]>
26991