Printing Hello World from multiple threads
#include <omp.h>
int main ()
{
int nthreads = 4;
omp_set_num_threads(nthreads);
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("Hello World from thread = %d", id);
printf(" with %d threads\n",omp_get_num_threads());
}
printf("all done, with hopefully %d threads\n",nthreads);
OpenMP program to generate fibonacci series
/* File: omp_fibo.c
*
* Purpose: Try to compute n Fibonacci numbers using OpenMP. Show
* what happens if we try to parallelize a loop
* with dependences among the iterations. The program
* has a serious bug.
*
* Compile: gcc -g -Wall -fopenmp -o omp_fibo omp_fibo.c
* Run: ./omp_fibo <number of threads> <number of Fibonacci numbers>
*
* Input: none
* Output: A list of Fibonacci numbers
*
* Note: If your output seems to be OK, try increasing the number of
* threads and/or n.
*
* IPP: Section 5.5.2 (pp. 227 and ff.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
void Usage(char prog_name[]);
int main(int argc, char* argv[]) {
int thread_count, n, i;
long long* fibo;
if (argc != 3) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
n = strtol(argv[2], NULL, 10);
fibo = malloc(n*sizeof(long long));
fibo[0] = fibo[1] = 1;
# pragma omp parallel for num_threads(thread_count)
for (i = 2; i < n; i++)
fibo[i] = fibo[i-1] + fibo[i-2];
printf("The first n Fibonacci numbers:\n");
for (i = 0; i < n; i++)
printf("%lld ", fibo[i]);
printf("\n");
free(fibo);
return 0;
} /* main */
void Usage(char prog_name[]) {
fprintf(stderr, "usage: %s <thread count> <number of Fibonacci
numbers>\n",
prog_name);
exit(0);
} /* Usage */
Matrix multiplication using OPENMP
/*
** PROGRAM: Parallel Matrix Multiply (using OpenMP)
**
** PURPOSE: This is a simple matrix multiply program.
** It will compute the product
**
** C = A * B
**
** A and B are set to constant matrices so we
** can make a quick test of the multiplication.
**
** USAGE: Right now, I hardwire the martix dimensions.
** later, I'll take them from the command line.
**
** HISTORY: Written by Tim Mattson, Nov 1999.
*/
#include <malloc.h>
#include <stdio.h>
#include <omp.h>
#define ORDER 1000
#define AVAL 3.0
#define BVAL 5.0
#define TOL 0.001
int main(int argc, char *argv[])
{
int Ndim, Pdim, Mdim; /* A[N][P], B[P][M], C[N][M] */
int i,j,k;
double *A, *B, *C, cval, tmp, err, errsq;
double dN, mflops;
double start_time, run_time;
Ndim = ORDER;
Pdim = ORDER;
Mdim = ORDER;
A = (double *)malloc(Ndim*Pdim*sizeof(double));
B = (double *)malloc(Pdim*Mdim*sizeof(double));
C = (double *)malloc(Ndim*Mdim*sizeof(double));
/* Initialize matrices */
for (i=0; i<Ndim; i++)
for (j=0; j<Pdim; j++)
*(A+(i*Ndim+j)) = AVAL;
for (i=0; i<Pdim; i++)
for (j=0; j<Mdim; j++)
*(B+(i*Pdim+j)) = BVAL;
for (i=0; i<Ndim; i++)
for (j=0; j<Mdim; j++)
*(C+(i*Ndim+j)) = 0.0;
start_time = omp_get_wtime();
/* Do the matrix product */
#pragma omp parallel for private(tmp, i, j, k)
for (i=0; i<Ndim; i++){
for (j=0; j<Mdim; j++){
tmp = 0.0;
for(k=0;k<Pdim;k++){
/* C(i,j) = sum(over k) A(i,k) * B(k,j) */
tmp += *(A+(i*Ndim+k)) * *(B+(k*Pdim+j));
}
*(C+(i*Ndim+j)) = tmp;
}
}
/* Check the answer */
run_time = omp_get_wtime() - start_time;
printf(" Order %d multiplication in %f seconds \n", ORDER,
run_time);
printf(" %d threads\n",omp_get_max_threads());
dN = (double)ORDER;
mflops = 2.0 * dN * dN * dN/(1000000.0* run_time);
printf(" Order %d multiplication at %f mflops\n", ORDER,
mflops);
cval = Pdim * AVAL * BVAL;
errsq = 0.0;
for (i=0; i<Ndim; i++){
for (j=0; j<Mdim; j++){
err = *(C+i*Ndim+j) - cval;
errsq += err * err;
}
}
if (errsq > TOL)
printf("\n Errors in multiplication: %f",errsq);
else
printf("\n Hey, it worked");
printf("\n all done \n");
}