Openmp Programming: Aiichiro Nakano
Openmp Programming: Aiichiro Nakano
Aiichiro Nakano!
Collaboratory for Advanced Computing & Simulations! Department of Computer Science! Department of Physics & Astronomy! Department of Chemical Engineering & Materials Science! University of Southern California! Email: [email protected]!
OpenMP !
!Portable application program interface (API) for shared-memory
!OpenMP = Open specications for Multi Processing! !OpenMP homepage !
! www.openmp.org!
!OpenMP tutorial !
! www.llnl.gov/computing/tutorials/openMP!
share
!Obtain the number of threads & my thread ID! !By default, all variables are shared unless selectively changing storage attributes using private clauses !
parallel section !
!PBS script!
!#!/bin/bash! !#PBS -l nodes=1:ppn=2,arch=x86_64! !#PBS -l walltime=00:00:59! Set the # of threads !#PBS -o omp_example.out! !#PBS -j oe! using environment !#PBS -N omp_example! parameter! !OMP_NUM_THREADS=2! !WORK_HOME=/home/rcf-proj2/an/anakano/hpc/cs596! !cd $WORK_HOME! !./omp_example!
!Output!
!Sequential section: # of threads = 1! !Parallel section: Hello world from thread 1! !Parallel section: Hello world from thread 0! !Parallel section: # of threads = 2!
!Setting the number of threads to be used in parallel sections within the program (no need to set OMP_NUM_THREADS); see omp_example_set.c!
Threads wait their turn only one at a time executes the critical section!
Example: Calculating ! !
!Numerical integration! 1 4 dx = # "0 2 1+ x !Discretization:! !" = 1/N: step = 1/NBIN! !xi = (i+0.5)" (i = 0,,N-1)!
N "1 i =01 +
4 xi2
$ %&
#include <stdio.h>! #define NBIN 100000! void main() {! !int i; double step,x,sum=0.0,pi;! !step = 1.0/NBIN;! !for (i=0; i<NBIN; i++) {! ! !x = (i+0.5)*step;! ! !sum += 4.0/(1.0+x*x);}! !pi = sum*step;! !printf( PI = %f\n ,pi);! }!