export OMP_NUM_THREADS=8 gcc -o code18 code18.c -fopenmp ./code18
1 /******************************************************************* 2 * FILE: omp_hello.c 3 * DESCRIPTION: 4 * OpenMP Example - Hello World - C/C++ Version 5 * In this simple example, the master thread forks a parallel region. 6 * All threads in the team obtain their unique thread number and 7 * print it. The master thread only prints the total number of threads. 8 * Two OpenMP library routines are used to obtain the number of 9 * threads and each thread's number. 10 * AUTHOR: Blaise Barney 5/99 11 * LAST REVISED: 04/06/05 12 ******************************************************************* */ 13 #include <omp.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 int main (int argc, char *argv[]) { 18 19 int nthreads, tid; 20 /* Fork a team of threads giving them their own copies of variables */ 21 #pragma omp parallel private(nthreads, tid) 22 { 23 tid = omp_get_thread_num(); /* Obtain thread number */ 24 printf("Hello World from thread : %d\n", tid); 25 26 /* Only master thread does this */ 27 if (tid == 0) 28 { 29 nthreads = omp_get_num_threads(); 30 printf("Number of threads = %d\n", nthreads); 31 } 32 33 } /* All threads join master thread and disband */ 34 return 0; 35 } |
export OMP_NUM_THREADS=8 gcc -o code19 code19.c -fopenmp ./code19
1 #include <stdio.h> 2 #include <omp.h> 3 #include <unistd.h> 4 5 int a,b,x,y,num_threads,thread_num; 6 int main() 7 { 8 printf("I am in sequential part.\n"); 9 #pragma omp parallel num_threads (8) private (a) shared (b) 10 { 11 num_threads=omp_get_num_threads(); 12 thread_num=omp_get_thread_num(); 13 x=thread_num; 14 //sleep(1); 15 y=x+1; 16 printf("I am openMP parellized part and thread %d. \n X and Y values are %d and %d. \n",omp_get_thread_num(),x,y); 17 } 18 printf("I am in sequential part again.\n"); 19 return 0; 20 } |
gcc -o code20 code20.c -fopenmp ./code20
1 /******************************************************************* 2 * FILE: omp_workshare1.c 3 * DESCRIPTION: 4 * OpenMP Example - Loop Work-sharing - C/C++ Version 5 * In this example, the iterations of a loop are scheduled dynamically 6 * across the team of threads. A thread will perform CHUNK iterations 7 * at a time before being scheduled for the next CHUNK of work. 8 * AUTHOR: Blaise Barney 5/99 9 * LAST REVISED: 04/06/05 10 ******************************************************************* */ 11 #include <omp.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #define CHUNKSIZE 10 15 #define N 100 16 17 int main (int argc, char *argv[]) { 18 19 int nthreads, tid, i, chunk; 20 float a[N], b[N], c[N]; 21 22 for (i=0; i < N; i++) /* Some initializations */ 23 a[i] = b[i] = i * 1.0; 24 chunk = CHUNKSIZE; 25 26 #pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid) 27 { 28 tid = omp_get_thread_num(); 29 if (tid == 0) { 30 nthreads = omp_get_num_threads(); 31 printf("Number of threads = %d\n", nthreads); 32 } 33 printf("Thread %d starting...\n",tid); 34 35 // #pragma omp for schedule(static,chunk) 36 #pragma omp for schedule(dynamic,chunk) 37 for (i=0; i<N; i++) { 38 c[i] = a[i] + b[i]; 39 printf("Thread %d: c[%d]= %f\n",tid,i,c[i]); 40 } 41 } /* end of parallel section */ 42 return 0; 43 } |