#include <omp.h>
#include <stdio.h>
main(int argc, char *argv[]) {
int nthreads, tid;
/* Fork a team of threads with each thread having a private tid variable */
#pragma omp parallel private(tid)
{
/* Obtain and print thread id */
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 master thread and terminate */
}
Hello world!
Let's print the statement "Hello world!" using all the threads. Here the variable tid
gives your thread id. Our server has only two threads. Threfore, the Hello world!
will be printed twice.
c++
To compile in your computer this code, run the following:
gcc -o omp_helloc -fopenmp omp_hello.c
$ export OMP_NUM_THREADS=2
$ ./omp_helloc
Hello World from thread = 0
Hello World from thread = 1
Number of threads = 2
Note that the environment variable OMP_NUM_THREADS
was used to specify number of threads.
Class Lessons
Structured Programming
Introduction
Variables, Literals and Constants
Data Types
Input and Output
Operators
Comments
Conditions
Switch
Goto
Functions
Recursion
Arrays
Strings
Structures
Pointers
Pointers and Arrays
Pointers and Functions
Pointers and Structures
Learn C Plus Plus
Objects and classes
Constructors
Memory Management
Polymorphism
Functions Overloading
Operator Overloading
Inheritance
Friend Function and Classes
Virtual Function
Templates
STL - STANDARD TEMPLATE LIBRARY
STL
Vectors
Lists
Map
Queue
Set
Pair
BOOST - BOOST Library
What is Boost?
Multiprecision
Boost - Any
Boost - Split
Boost - Tuple
Boost - Strings
Boost - Is Sorted
Boost - None of
Boost - Is Partitioned
Boost - Equal
Boost - One of
Boost - Any of
Boost - All of
OPENMP - Parallel Programming
OpenMP
OpenMP- Hello world!
OpenMP - Reduction
OpenMP- Section Parallelization
OpenMP - Vector addition
OpneMP Parallelisation - Pi value
MPI - Message Passing Interface
MPI
MPI Program
MPI Hello World
MPI Send and receive
CUDA - GPU Programming
GPUs
CUDA
CUDA Threads
CUDA Memory
CUDA Kernel
CUDA Hello world
CUDA Array
CUDA Vector addition
CUDA Matrix multiplication
C++ Scientific Programming
Intel Math Kernel Library
MKL Matrix Multiplication
Linear Algebra Package
Conclusions
Conclusion