Difference between revisions of "Programming/C"

From HPC
Jump to: navigation , search
m (Pysdlb moved page C to Programming/C without leaving a redirect)
m (Navigation)
 
(38 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Programming Details ==
 
== Programming Details ==
  
C is a general-purpose, imperative computer programming language. There are some derivations of the language due to the modernisation of the original specification in the form of C++ and C#.
+
C is a general-purpose, imperative computer programming language. There are some derivations of the language due to the modernization of the original specification in the form of C++ and C#.
 
+
{|
There is direct support for openMP and openMPI.
+
|style="width:5%; border-width: 0;cellpadding=0" | [[File:icon_tick.png]]
 
+
|style="width:95%; border-width: 0;cellpadding=0" | There is direct support for [[programming/OpenMP|openMP]] and [[programming/OpenMPI|openMPI]].
 +
|-
 +
|}
 +
=== Programming example ===
  
  
=== Programming example ===
+
<pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;">
 
 
<pre style="background-color: #C8C8C8; color: black; border: 2px solid green; font-family: monospace, sans-serif;">
 
  
 
#include <omp.h>
 
#include <omp.h>
Line 45: Line 46:
  
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 
+
[username@login01 ~]$  module add gcc/8.2.0
[username@login01 ~]$  module load gcc/4.9.3
 
 
[username@login01 ~]$  gcc -o test2 -fopenmp test2.c (openMP support)
 
[username@login01 ~]$  gcc -o test2 -fopenmp test2.c (openMP support)
 
[username@login01 ~]$ ./test2
 
[username@login01 ~]$ ./test2
Line 52: Line 52:
 
Hello World from thread = 0
 
Hello World from thread = 0
 
Number of threads = 1
 
Number of threads = 1
 
 
</pre>
 
</pre>
  
Line 59: Line 58:
 
The following modules are available for C
 
The following modules are available for C
  
* module load gcc/4.9.3 (GNU compiler)
+
* module add gcc/8.2.0 (GNU compiler)
* module load intel/compiler/64/2016.2.181 (Intel compiler)
+
* module add intel/compiler/64/2016.2.181 (Intel compiler)
 +
{|
 +
|style="width:5%; border-width: 0;cellpadding=0" | [[File:icon_exclam3.png]]
 +
|style="width:95%; border-width: 0;cellpadding=0" | OpenMP is a library directive within the compiler and does not require any additional module to be loaded.
 +
|-
 +
|}
  
 +
== Usage Examples ==
  
<strong>Note</strong>: OpenMP is a library directive within the compiler and does not require any additional module to be loaded.
+
== Batch example ==
 
 
  
== Usage Examples ==
 
  
== Batch example ==
+
<pre style="background-color: #C8C8C8; color: black; border: 2px solid #C8C8C8; font-family: monospace, sans-serif;">
  
<pre style="background-color: #C8C8C8; color: black; border: 2px solid blue; font-family: monospace, sans-serif;">
 
 
#!/bin/bash
 
#!/bin/bash
  
Line 76: Line 78:
 
#SBATCH -N 1
 
#SBATCH -N 1
 
#SBATCH --ntasks-per-node 28
 
#SBATCH --ntasks-per-node 28
#SBATCH -D /home/user/CODE_SAMPLES/OPENMP
 
 
#SBATCH -o %N.%j.%a.out
 
#SBATCH -o %N.%j.%a.out
 
#SBATCH -e %N.%j.%a.err
 
#SBATCH -e %N.%j.%a.err
 
#SBATCH -p compute
 
#SBATCH -p compute
 
#SBATCH --exclusive
 
#SBATCH --exclusive
 +
#SBATCH --mail-user= your email address here
  
 
echo $SLURM_JOB_NODELIST
 
echo $SLURM_JOB_NODELIST
  
 
module purge
 
module purge
module load gcc/4.9.3
+
module add gcc/8.2.0
  
 
export I_MPI_DEBUG=5
 
export I_MPI_DEBUG=5
Line 92: Line 94:
  
 
/home/user/CODE_SAMPLES/OPENMP/demo
 
/home/user/CODE_SAMPLES/OPENMP/demo
 
 
</pre>
 
</pre>
  
Line 101: Line 102:
 
</pre>
 
</pre>
  
== Further Information ==
+
== Next Steps ==
  
 
* [https://en.wikipedia.org/wiki/C_(programming_language) https://en.wikipedia.org/wiki/C_(programming_language)]
 
* [https://en.wikipedia.org/wiki/C_(programming_language) https://en.wikipedia.org/wiki/C_(programming_language)]
Line 108: Line 109:
 
* [https://computing.llnl.gov/tutorials/openMP/ https://computing.llnl.gov/tutorials/openMP/]
 
* [https://computing.llnl.gov/tutorials/openMP/ https://computing.llnl.gov/tutorials/openMP/]
  
[[Category:Programming]]
+
{{Languagespagenav}}

Latest revision as of 11:25, 16 November 2022

Programming Details

C is a general-purpose, imperative computer programming language. There are some derivations of the language due to the modernization of the original specification in the form of C++ and C#.

Icon tick.png There is direct support for openMP and openMPI.

Programming example


#include <omp.h>
#include <stdio.h>

int main (int argc, char *argv[]){
        int nthreads, tid;

        /* Fork a team of threads giving them their own copies of variables*/
        #pragma omp parallel
        {
                /* Obtain thread number */
                tid = omp_get_thread_num();
                printf("Hello World from thread = %d\n", tid);

                /* Only Master thread does this */
                if(tid == 0)
                {
                        nthreads = omp_get_num_threads();
                        printf("Number of threads = %d\n", nthreads);
                }

        }       /* All threads join together and disband */

        return (0);
}


Compilation

The program would be compiled in the following way, optional Intel compiler available too:


[username@login01 ~]$  module add gcc/8.2.0
[username@login01 ~]$  gcc -o test2 -fopenmp test2.c (openMP support)
[username@login01 ~]$ ./test2

Hello World from thread = 0
Number of threads = 1

Modules Available

The following modules are available for C

  • module add gcc/8.2.0 (GNU compiler)
  • module add intel/compiler/64/2016.2.181 (Intel compiler)
Icon exclam3.png OpenMP is a library directive within the compiler and does not require any additional module to be loaded.

Usage Examples

Batch example


#!/bin/bash

#SBATCH -J openmpi-single-node
#SBATCH -N 1
#SBATCH --ntasks-per-node 28
#SBATCH -o %N.%j.%a.out
#SBATCH -e %N.%j.%a.err
#SBATCH -p compute
#SBATCH --exclusive
#SBATCH --mail-user= your email address here

echo $SLURM_JOB_NODELIST

module purge
module add gcc/8.2.0

export I_MPI_DEBUG=5
export I_MPI_FABRICS=shm:tmi
export I_MPI_FALLBACK=no

/home/user/CODE_SAMPLES/OPENMP/demo


[username@login01 ~]$ sbatch demo.job
Submitted batch job 289552

Next Steps





Languages | Main Page | Further Topics