[550a3385] | 1 | |
---|
| 2 | #include <fstream> |
---|
[a91dcc2] | 3 | #include <stdlib> |
---|
[29f44a74] | 4 | #include <thread> |
---|
[550a3385] | 5 | |
---|
[8cb6fcd] | 6 | #include "bench.h" |
---|
[550a3385] | 7 | |
---|
[a91dcc2] | 8 | //======================================= |
---|
| 9 | // time struct |
---|
| 10 | //======================================= |
---|
| 11 | |
---|
| 12 | // prevent dead-code removal |
---|
| 13 | struct StructDummy { |
---|
| 14 | volatile int i; |
---|
| 15 | }; |
---|
| 16 | |
---|
| 17 | void ?{}(StructDummy * this) __attribute__(( noinline )) { |
---|
| 18 | this->i = 2; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | int get_i(StructDummy * this) { |
---|
| 22 | return this->i; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | forall( dtype T | { int get_i(T*); } ) |
---|
| 26 | int bidirectional( StructDummy * this ) __attribute__(( noinline )) { |
---|
| 27 | return get_i( this ); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | void BlockStructCreateDelete( int N ) { |
---|
| 31 | long long int StartTime, EndTime; |
---|
| 32 | |
---|
| 33 | StartTime = Time(); |
---|
| 34 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 35 | StructDummy dummy; |
---|
| 36 | } |
---|
| 37 | EndTime = Time(); |
---|
| 38 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void DynamicStructCreateDelete( int N ) { |
---|
| 42 | long long int StartTime, EndTime; |
---|
| 43 | |
---|
| 44 | StartTime = Time(); |
---|
| 45 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 46 | StructDummy *dummy = new(); |
---|
| 47 | delete(dummy); |
---|
| 48 | } |
---|
| 49 | EndTime = Time(); |
---|
| 50 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void StructBidirectional( int N ) { |
---|
| 54 | long long int StartTime, EndTime; |
---|
| 55 | StructDummy dummy; |
---|
| 56 | volatile int rv __attribute__(( unused )); |
---|
| 57 | |
---|
| 58 | StartTime = Time(); |
---|
| 59 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 60 | rv = bidirectional( &dummy ); |
---|
| 61 | } |
---|
| 62 | EndTime = Time(); |
---|
| 63 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 64 | } |
---|
| 65 | |
---|
[550a3385] | 66 | //======================================= |
---|
| 67 | // time coroutine |
---|
| 68 | //======================================= |
---|
| 69 | |
---|
[b510ac2] | 70 | coroutine CoroutineDummy {}; |
---|
[a91dcc2] | 71 | void main(CoroutineDummy * this) {} |
---|
| 72 | |
---|
| 73 | void ?{}(CoroutineDummy * this) { |
---|
| 74 | prime(this); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void BlockCoroutineCreateDelete( int N ) { |
---|
| 78 | long long int StartTime, EndTime; |
---|
| 79 | |
---|
| 80 | StartTime = Time(); |
---|
| 81 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 82 | CoroutineDummy dummy; |
---|
| 83 | } |
---|
| 84 | EndTime = Time(); |
---|
| 85 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void DynamicCoroutineCreateDelete( int N ) { |
---|
| 89 | long long int StartTime, EndTime; |
---|
| 90 | |
---|
| 91 | StartTime = Time(); |
---|
| 92 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 93 | CoroutineDummy * dummy = new(); |
---|
| 94 | delete(dummy); |
---|
| 95 | } |
---|
| 96 | EndTime = Time(); |
---|
| 97 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 98 | } |
---|
| 99 | |
---|
[b510ac2] | 100 | coroutine CoroutineResume { |
---|
[550a3385] | 101 | int N; |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | void ?{}(CoroutineResume* this, int N) { |
---|
| 105 | this->N = N; |
---|
| 106 | prime(this); |
---|
| 107 | } |
---|
| 108 | |
---|
[a91dcc2] | 109 | void main(CoroutineResume* this) { |
---|
[550a3385] | 110 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
| 111 | suspend(); |
---|
[a91dcc2] | 112 | } |
---|
| 113 | } |
---|
[550a3385] | 114 | |
---|
| 115 | void resumer(CoroutineResume* this) { |
---|
| 116 | long long int StartTime, EndTime; |
---|
| 117 | |
---|
| 118 | StartTime = Time(); |
---|
| 119 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
| 120 | resume(this); |
---|
[a91dcc2] | 121 | } |
---|
[550a3385] | 122 | EndTime = Time(); |
---|
| 123 | sout | "\t " | ( EndTime - StartTime ) / this->N; |
---|
[a91dcc2] | 124 | } |
---|
| 125 | |
---|
| 126 | //======================================= |
---|
| 127 | // time task |
---|
| 128 | //======================================= |
---|
| 129 | |
---|
[b510ac2] | 130 | thread ThreadDummy {}; |
---|
[a91dcc2] | 131 | void main(ThreadDummy * this) {} |
---|
| 132 | |
---|
| 133 | void BlockTaskCreateDelete( int N ) { |
---|
| 134 | long long int StartTime, EndTime; |
---|
| 135 | |
---|
| 136 | StartTime = Time(); |
---|
| 137 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 138 | scoped(ThreadDummy) dummy; |
---|
| 139 | } |
---|
| 140 | EndTime = Time(); |
---|
| 141 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void DynamicTaskCreateDelete( int N ) { |
---|
| 145 | long long int StartTime, EndTime; |
---|
| 146 | |
---|
| 147 | StartTime = Time(); |
---|
| 148 | for ( int i = 0; i < N; i += 1 ) { |
---|
| 149 | scoped(ThreadDummy) * dummy = new(); |
---|
| 150 | delete(dummy); |
---|
| 151 | } |
---|
| 152 | EndTime = Time(); |
---|
| 153 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
| 154 | } |
---|
| 155 | |
---|
[b510ac2] | 156 | thread ContextSwitch { |
---|
[a91dcc2] | 157 | int N; |
---|
| 158 | long long result; |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | void main(ContextSwitch * this) { |
---|
| 162 | long long int StartTime, EndTime; |
---|
| 163 | |
---|
| 164 | StartTime = Time(); |
---|
| 165 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
| 166 | yield(); |
---|
| 167 | } // for |
---|
| 168 | EndTime = Time(); |
---|
| 169 | this->result = ( EndTime - StartTime ) / this->N; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void ?{}(ContextSwitch * this, int N) { |
---|
| 173 | this->N = N; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | void ^?{}(ContextSwitch * this) { |
---|
| 177 | sout | "\t " | this->result; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //======================================= |
---|
| 181 | // benchmark driver |
---|
| 182 | //======================================= |
---|
[550a3385] | 183 | |
---|
| 184 | |
---|
| 185 | int main() { |
---|
| 186 | const int NoOfTimes = |
---|
| 187 | #if defined( __U_DEBUG__ ) // takes longer so run fewer iterations |
---|
| 188 | 100000; |
---|
| 189 | #else |
---|
| 190 | 1000000; |
---|
| 191 | #endif // __U_DEBUG__ |
---|
| 192 | |
---|
[a91dcc2] | 193 | sout | "\t\tcreate\tcreate\tcall/" | endl; |
---|
[550a3385] | 194 | sout | "(nsecs)"; |
---|
[a91dcc2] | 195 | sout | "\t\tdelete/\tdelete/\tctx" | endl; |
---|
| 196 | sout | "\t\tblock\tdynamic\tcycle" | endl; |
---|
[550a3385] | 197 | |
---|
[a91dcc2] | 198 | sout | "object\t"; |
---|
| 199 | BlockStructCreateDelete( NoOfTimes ); |
---|
| 200 | DynamicStructCreateDelete( NoOfTimes ); |
---|
| 201 | StructBidirectional( NoOfTimes ); |
---|
| 202 | sout | endl; |
---|
[550a3385] | 203 | |
---|
| 204 | sout | "coroutine"; |
---|
[a91dcc2] | 205 | BlockCoroutineCreateDelete( NoOfTimes ); |
---|
| 206 | DynamicCoroutineCreateDelete( NoOfTimes ); |
---|
[550a3385] | 207 | { |
---|
| 208 | CoroutineResume resumer = { NoOfTimes }; |
---|
| 209 | resumer(&resumer); |
---|
| 210 | } |
---|
[a91dcc2] | 211 | sout | endl; |
---|
[550a3385] | 212 | |
---|
[a91dcc2] | 213 | sout | "task\t"; |
---|
| 214 | BlockTaskCreateDelete( NoOfTimes ); |
---|
| 215 | DynamicTaskCreateDelete( NoOfTimes ); |
---|
| 216 | { |
---|
[b510ac2] | 217 | ContextSwitch dummy = { (int)NoOfTimes }; // context switch |
---|
[a91dcc2] | 218 | } |
---|
[550a3385] | 219 | sout | "\t" | endl; |
---|
| 220 | } |
---|