#ifdef _CONSOLE #include #include #include #include #else #include "omp.h" #endif #define NPROCS 2 /*set to number of physical processors*/ void test(); /* Copyright (C) by Bob Cook , 2006. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. Write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #define N 100 omp_lock_t olt; omp_nest_lock_t onlt; int global,isDynamic,isNested; int fact(int n) { omp_set_nest_lock(&onlt); printf("\tsuccessful\n"); n=(n>1)?fact(n-1)*n:1; omp_unset_nest_lock(&onlt); return n; } int main(int argc, char **argv) { float z,w[200]; int i,j,k,m,x[N][N]; char *s; double start,stop,local; #ifndef _OPENMP printf("_OPENMP not defined err#1\n"); #endif printf("wtime=%lg wtick=%lg max_threads=%d dynamic=%d nested=%d\n",omp_get_wtime(),omp_get_wtick(),omp_get_max_threads(),omp_get_dynamic(),omp_get_nested()); isDynamic=omp_get_dynamic(); isNested=omp_get_nested(); s=getenv("OMP_NUM_THREADS"); if (s!=NULL) { i=atoi(s); if (i!=omp_get_num_threads()) printf("OMP_NUM_THREADS wasn't used properly err#2\n"); } s=getenv("OMP_DYNAMIC"); if (s!=NULL) { i=atoi(s); if (i!=omp_get_dynamic()) printf("OMP_DYNAMIC wasn't used properly err#3\n"); } s=getenv("OMP_NESTED"); if (s!=NULL) { i=atoi(s); if (i!=omp_get_nested()) printf("OMP_NESTED wasn't used properly err#4\n"); } if (omp_in_parallel()) printf("omp_in_parallel err#5\n"); omp_set_dynamic(0); omp_set_num_threads(2); if (omp_get_num_threads()!=2) printf("omp_get_num_threads should be 2 err#6\n"); if (omp_get_thread_num()!=0) printf("omp_get_thread_num should be 0 err#7\n"); if (omp_get_num_procs()!=NPROCS) printf("omp_get_num_procs should be %d err#8\n",NPROCS); omp_init_lock(&olt); printf("omp_set_lock\n"); omp_set_lock(&olt); omp_unset_lock(&olt); printf("\tsuccessful\n"); if (!omp_test_lock(&olt)) printf("omp_test_lock err#9\n"); omp_unset_lock(&olt); omp_destroy_lock(&olt); omp_init_nest_lock(&onlt); printf("omp_set_nest_lock 5\n"); i=fact(5); if (!omp_test_nest_lock(&onlt)) printf("omp_test_nest_lock err#10\n"); omp_destroy_nest_lock(&onlt); printf("%d threads should start\n",omp_get_num_threads()); #pragma omp parallel { printf("Thread %d running.\n",omp_get_thread_num()); } printf("%d threads should have stopped\n",omp_get_num_threads()); printf("only 1 thread should start\n"); #pragma omp parallel if (omp_get_num_threads()<0) { printf("Thread %d running.\n",omp_get_thread_num()); } printf("1 thread should have stopped\n"); i=omp_get_num_threads(); printf("%d threads should start\n",i+2); j=0; k=1; m=0; start=omp_get_wtime(); #pragma omp parallel if (omp_get_num_threads()>0) num_threads( omp_get_num_threads()+2 ) { printf("Thread %d running.\n",omp_get_thread_num()); #pragma omp master { j++; if (omp_get_thread_num()!=0) printf("omp master err#12\n"); } #pragma omp single nowait { if (m!=0) printf("omp single err#14\n"); m=omp_get_thread_num()+1; Sleep(1000); } } stop=omp_get_wtime(); if (j!=1) printf("omp master err#13\n"); if (i!=omp_get_num_threads()) printf("omp_get_num_threads not reset err#11\n"); if (m==0) printf("omp single err#15\n"); if (j==0) printf("omp master err#16\n"); if ((stop-start)>1.3) printf("omp single err#17\n"); printf("%d threads should have stopped\n",omp_get_num_threads()+2); printf("%d threads should start\n",omp_get_num_threads()); start=omp_get_wtime(); #pragma omp parallel { printf("Thread %d running.\n",omp_get_thread_num()); #pragma omp critical { Sleep(500); } } stop=omp_get_wtime(); if ((stop-start)< (omp_get_num_threads()*0.5)) printf("omp critical err#18 %lg\n",stop-start); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); j=0; start=0; z=0; #pragma omp parallel private(i) { printf("Thread %d running.\n",omp_get_thread_num()); for (i=0; i<10000; i++) { #pragma omp atomic start++; if ((i%1000)==0) Sleep(1); #pragma omp atomic j++; #pragma omp atomic z++; } } if (j!=(omp_get_num_threads()*10000)) printf("omp atomic int err#19 %d\n",j); if (start!=(omp_get_num_threads()*10000)) printf("omp atomic double err#20 %lg\n",start); if (z!=(omp_get_num_threads()*10000)) printf("omp atomic float err#21 %g\n",z); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); j=0; start=0; z=0; #pragma omp parallel private(i) { printf("Thread %d running.\n",omp_get_thread_num()); for (i=0; i<10000; i++) { #pragma omp atomic start+=3.3; #pragma omp atomic z-=(float)3.3; #pragma omp atomic j-=1234; if ((i%1000)==0) Sleep(1); #pragma omp atomic j+=1234; #pragma omp atomic start-=3.3; #pragma omp atomic z+=(float)3.3; } } if (j!=0) printf("omp atomic int err#22 %d\n",j); if (start!=0) printf("omp atomic double err#23 %lg\n",start); if (z!=0) printf("omp atomic float err#24 %g\n",z); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); j=1; start=1; z=1; #pragma omp parallel private(i) { printf("Thread %d running.\n",omp_get_thread_num()); for (i=0; i<10000; i++) { #pragma omp atomic start*=12345; #pragma omp atomic z*=12345; #pragma omp atomic j*=12345; if ((i%1000)==0) Sleep(1); #pragma omp atomic j/=12345; #pragma omp atomic start/=12345; #pragma omp atomic z/=12345; } } if (j!=1) printf("omp atomic int err#25 %d\n",j); if (start!=1) printf("omp atomic double err#26 %lg\n",start); if (z!=1) printf("omp atomic float err#27 %g\n",z); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); j=0; m=0; #pragma omp parallel private(i) { printf("Thread %d running.\n",omp_get_thread_num()); for (i=0; i<10000; i++) { #pragma omp atomic j|=0x120000; #pragma omp atomic j&=0xffff00; if ((i%1000)==0) Sleep(1); #pragma omp atomic j|=0x3456; #pragma omp atomic m^=0x123456; } } if (j!=0x123456) printf("omp atomic int err#28 %d\n",j); if (m!=0) printf("omp atomic int err#29 %d\n",m); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); global=0; local=0; #pragma omp parallel { printf("Thread %d running.\n",omp_get_thread_num()); if (omp_get_thread_num()==1) { global=99; local=999; #pragma omp flush } else { #pragma omp master printf("omp flush err#30 if no progress\n"); while ((global!=99) && (local!=999)) Sleep(1); } } printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); global=0; local=0; #pragma omp parallel { printf("Thread %d running.\n",omp_get_thread_num()); if (omp_get_thread_num()==1) { global=99; #pragma omp flush local=999; #pragma omp flush(global,local) } else { #pragma omp master printf("omp flush(...) err#31 if no progress\n"); while ((global!=99) && (local!=999)) Sleep(1); } } printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); global=55; local=5555; #pragma omp parallel { printf("Thread %d running.\n",omp_get_thread_num()); Sleep(rand()&0xff); if ((global!=55)||(local!=5555)) printf("omp barrier err#32\n"); #pragma omp barrier global=44; local=4444; #pragma omp flush } if ((global!=44)||(local!=4444)) printf("omp barrier err#33\n"); printf("%d threads should have stopped\n",omp_get_num_threads()); printf("%d threads should start\n",omp_get_num_threads()); i=0; #pragma omp parallel { #pragma omp sections { #pragma omp section { printf("Thread %d running section 1.\n",omp_get_thread_num()); i+=1; } #pragma omp section { printf("Thread %d running section 2.\n",omp_get_thread_num()); i+=4; } #pragma omp section { printf("Thread %d running section 3.\n",omp_get_thread_num()); i+=16; } } } printf("%d threads should have stopped\n",omp_get_num_threads()); if (i!=(16+4+1)) printf("omp section err#34\n"); printf("3 threads should start 0-012 1-345 2-678\n"); j=0; #pragma omp parallel for schedule(static) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched static err#35\n"); printf("3 threads should start, round-robin by 1 by thread#\n"); j=0; #pragma omp parallel for schedule(static,1) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched static,1 err#36\n"); printf("3 threads should start 0-0123 1-4567 2-8\n"); j=0; #pragma omp parallel for schedule(static,4) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched static,4 err#37\n"); printf("3 threads should start 0-9870(-1) 1-654 2-321\n"); j=0; #pragma omp parallel for schedule(static,3) num_threads(3) for (i=9; i>-2; i=i-1) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=11) printf("omp sched static,3 err#38\n"); printf("up to 3 threads should start, assigned 2 each as requested\n"); j=0; #pragma omp parallel for schedule(dynamic,2) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched dynamic,2 err#39\n"); printf("up to 3 threads should start, assigned proportional to remaining iterations div 3\n"); j=0; #pragma omp parallel for schedule(guided) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched guided err#40\n"); printf("up to 3 threads should start, assigned proportional to remaining iterations div 3, with no chunk<2\n"); j=0; #pragma omp parallel for schedule(guided,2) num_threads(3) for (i=0; i<9; i++) { printf("Thread %d running %d.\n",omp_get_thread_num(),i); #pragma omp atomic j++; } printf("3 threads should have stopped\n"); if (j!=9) printf("omp sched guided,2 err#41\n"); j=0; #pragma omp parallel for reduction(+:j) num_threads(3) for (i=0; i<9; i++) { j+=2; } if (j!=18) printf("omp reduction + %d err#42\n",j); j=0; #pragma omp parallel for reduction(-:j) num_threads(3) for (i=0; i<9; i++) { j-=2; } if (j!=-18) printf("omp reduction - %d err#43\n",j); j=1; #pragma omp parallel for reduction(*:j) num_threads(3) for (i=0; i<9; i++) { j*=2; } if (j!=(1<<9)) printf("omp reduction * %d err#44\n",j); j=~0; #pragma omp parallel for reduction(&:j) num_threads(3) for (i=0; i<9; i++) { j&=(~0)^(1<