#include <omp.h> #include <stdio.h> #include <stdlib.h> void Test (int n) { int i; for (i = 0; i < 10000; ++i) { //do nothing, just waste time } printf("%d, ", n); } int main(int argc, char* argv[]) { int i;
#pragma omp parallel for for (i = 0; i < 10; ++i) Test( i ); return 1; }
#include <omp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { int nthreads,tid; // fork a team of thread #pragma omp parallel private(nthreads,tid) { //obtian and print thread id tid=omp_get_thread_num(); printf("Hello Word from OMP thread %d\n",tid);
// only master thread does this; if(tid==0) { nthreads = omp_get_num_threads(); printf("Number of thread: %d\n",nthreads); } } return 0; }
编译执行: [ycdoit@ycdoit Lu]$ gcc -fopenmp main.c -o main.out [ycdoit@ycdoit Lu]$ ./main.out Hello Word from OMP thread 0 Number of thread: 2 Hello Word from OMP thread 1