openmp1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #ifdef _OPENMP
  4. # include <omp.h>
  5. #endif
  6. #include <pthread.h>
  7. enum {
  8. Size = 10000
  9. };
  10. const int ShouldSum = (Size-1)*Size/2;
  11. short Verbose = 1;
  12. short ThreadOK[3] = {0,0,0}; // Main, Thread1, Thread2
  13. // Thread
  14. void *_thread(void* Id) {
  15. int i;
  16. int x[Size];
  17. #ifdef _OPENMP
  18. # pragma omp parallel for
  19. #endif
  20. for ( i = 0; i < Size; i++ ) {
  21. #ifdef _OPENMP
  22. if (Verbose && i%1000==0) {
  23. int tid = omp_get_thread_num();
  24. # pragma omp critical
  25. printf("thread %d : tid %d handles %d\n",(int)Id,tid,i);
  26. }
  27. #endif
  28. x[i] = i;
  29. }
  30. int Sum=0;
  31. for ( i = 0; i < Size; i++ ) {
  32. Sum += x[i];
  33. }
  34. if (Verbose) {
  35. #ifdef _OPENMP
  36. # pragma omp critical
  37. #endif
  38. printf("Id %d : %s : %d(should be %d)\n",(int)Id, __FUNCTION__, Sum,ShouldSum);
  39. }
  40. if (Sum == ShouldSum) ThreadOK[(int)Id] = 1;
  41. return NULL;
  42. }
  43. // MainThread
  44. void MainThread() {
  45. int i;
  46. #ifdef _OPENMP
  47. # pragma omp parallel for
  48. #endif
  49. for ( i = 0; i < 4; i++ ) {
  50. #ifdef _OPENMP
  51. int tid = omp_get_thread_num();
  52. # pragma omp critical
  53. printf("Main : tid %d\n",tid);
  54. _thread((void *)tid);
  55. #endif
  56. }
  57. return;
  58. }
  59. // Comment in/out for checking the effect of multiple threads.
  60. #define SPAWN_THREADS
  61. // main
  62. int main(int argc, char *argv[]) {
  63. if (argc>1) Verbose = 1;
  64. #ifdef _OPENMP
  65. omp_set_nested(-1);
  66. printf("%s%s%s\n", "Nested parallel blocks are ", omp_get_nested()?" ":"NOT ", "supported.");
  67. #endif
  68. MainThread();
  69. #ifdef SPAWN_THREADS
  70. {
  71. pthread_t a_thr;
  72. pthread_t b_thr;
  73. int status;
  74. printf("%s:%d - %s - a_thr:%p - b_thr:%p\n",
  75. __FILE__,__LINE__,__FUNCTION__,a_thr.p,b_thr.p);
  76. status = pthread_create(&a_thr, NULL, _thread, (void*) 1 );
  77. if ( status != 0 ) {
  78. printf("Failed to create thread 1\n");
  79. return (-1);
  80. }
  81. status = pthread_create(&b_thr, NULL, _thread, (void*) 2 );
  82. if ( status != 0 ) {
  83. printf("Failed to create thread 2\n");
  84. return (-1);
  85. }
  86. status = pthread_join(a_thr, NULL);
  87. if ( status != 0 ) {
  88. printf("Failed to join thread 1\n");
  89. return (-1);
  90. }
  91. printf("Joined thread1\n");
  92. status = pthread_join(b_thr, NULL);
  93. if ( status != 0 ) {
  94. printf("Failed to join thread 2\n");
  95. return (-1);
  96. }
  97. printf("Joined thread2\n");
  98. }
  99. #endif // SPAWN_THREADS
  100. short OK = 0;
  101. // Check that we have OpenMP before declaring things OK formally.
  102. #ifdef _OPENMP
  103. OK = 1;
  104. {
  105. short i;
  106. for (i=0;i<3;i++) OK &= ThreadOK[i];
  107. }
  108. if (OK) printf("OMP : All looks good\n");
  109. else printf("OMP : Error\n");
  110. #else
  111. printf("OpenMP seems not enabled ...\n");
  112. #endif
  113. return OK?0:1;
  114. }
  115. //g++ -fopenmp omp_test.c -o omp_test -lpthread