ms-queue: cleanups
[model-checker-benchmarks.git] / queue / test_lock_free_single_producer_q.cpp
1 // ============================================================================
2 /// @file  test_lock_free_q.cpp
3 /// @brief Benchmark lock free queue
4 // ============================================================================
5
6
7 #include <iostream>
8 #include <glib.h>  // GTimeVal + g_get_current_time
9 #include <omp.h>   // parallel processing support in gcc
10 #include "array_lock_free_queue_single_producer.h"
11
12 #ifndef N_CONSUMERS
13 #define N_CONSUMERS         1
14 #endif
15
16 #ifndef N_ITERATIONS
17 #define N_ITERATIONS 10000000
18 #endif
19
20 #ifndef QUEUE_SIZE
21 #define QUEUE_SIZE       1000
22 #endif
23
24 void TestLockFreeQueue()
25 {
26     ArrayLockFreeQueueSingleProducer<int, QUEUE_SIZE> theQueue;
27     GTimeVal iniTimestamp;
28     GTimeVal endTimestamp;
29
30     std::cout << "=== Start of testing lock-free queue ===" << std::endl;
31     g_get_current_time(&iniTimestamp);
32     #pragma omp parallel shared(theQueue) num_threads (2)
33     {
34         if (omp_get_thread_num() == 0)
35         {
36             //std::cout << "=== Testing Non blocking queue with " << omp_get_num_threads() << " threads ===" << std::endl;
37
38             if (!omp_get_nested())
39             {
40                 std::cerr << "WARNING: Nested parallel regions not supported. Working threads might have unexpected behaviour" << std::endl;
41                 std::cerr << "Are you running with \"OMP_NESTED=TRUE\"??" << std::endl;
42             }
43         }
44
45         #pragma omp sections //nowait
46         {
47             #pragma omp section
48             {
49                 // producer section. only 1 thread
50                 //#pragma omp parallel shared(theQueue) num_threads (1)
51                 {
52                     //if (omp_get_thread_num() == 0)
53                     //{
54                     //    std::cout << "\t Producers: " << omp_get_num_threads() << std::endl;
55                     //}
56                     int i;
57                     for (i = 0 ; i < N_ITERATIONS ; i++)
58                     {
59                         while(!theQueue.push(i))
60                         {
61                             // queue full
62                             ;
63                         }
64                     }
65                 }
66             }
67
68             #pragma omp section
69             {
70                 // consumer section
71                 #pragma omp parallel shared(theQueue) num_threads (N_CONSUMERS)
72                 {
73                     //if (omp_get_thread_num() == 0)
74                     //{
75                     //    std::cout << "\t Consumers: " << omp_get_num_threads() << std::endl;
76                     //}
77
78                     int i;
79                     int result;
80                     #pragma omp for schedule(static) private(i, result) nowait
81                     for (i = 0 ; i < N_ITERATIONS ; i++)
82                     {
83                         while (!theQueue.pop(result))
84                         {
85                             // queue empty
86                             ;
87                         }
88
89 #if N_CONSUMERS == 1
90                         // if there are several consumers this test will fail
91                         if (i != result)
92                         {
93                             std::cout << "FAILED i=" << i << " result=" << result << std::endl;
94                         }
95 #endif
96                     }
97                 }
98             }
99         }
100
101     } // #pragma omp parallel
102
103     g_get_current_time(&endTimestamp);
104     
105     // calculate elapsed time
106     GTimeVal elapsedTime;
107     if (endTimestamp.tv_usec >= iniTimestamp.tv_usec)
108     {
109         elapsedTime.tv_sec  = endTimestamp.tv_sec  - iniTimestamp.tv_sec;
110         elapsedTime.tv_usec = endTimestamp.tv_usec - iniTimestamp.tv_usec;
111     }
112     else
113     {
114         elapsedTime.tv_sec  = endTimestamp.tv_sec  - iniTimestamp.tv_sec - 1;
115         elapsedTime.tv_usec = G_USEC_PER_SEC + endTimestamp.tv_usec - iniTimestamp.tv_usec;
116     }
117     
118     std::cout << "Elapsed: " << elapsedTime.tv_sec << "." << elapsedTime.tv_usec << std::endl;
119     std::cout << "=== End of testing lock-free queue ===" << std::endl;    
120 }
121
122 int main(int /*argc*/, char** /*argv*/)
123 {
124
125         TestLockFreeQueue();
126     std::cout << "Done!!!" << std::endl;
127
128         return 0;
129 }
130