-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathTaskThroughput.cpp
163 lines (125 loc) · 4.6 KB
/
TaskThroughput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2013 Doug Binks
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgement in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
#include "TaskScheduler.h"
#include "Timer.h"
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
#include <cstring>
#include <string.h>
using namespace enki;
const uint32_t numTasks = 1024*1024;
TaskScheduler g_TS;
struct ConsumeTask : ITaskSet
{
static ConsumeTask tasks[numTasks];
struct Count
{
// prevent false sharing.
uint32_t count;
char cacheline[64];
};
static Count* pCount;
static uint32_t numCount;
void ExecuteRange( TaskSetPartition range_, uint32_t threadnum_ ) override
{
++pCount[threadnum_].count;
}
static void Init()
{
delete[] ConsumeTask::pCount;
numCount = g_TS.GetNumTaskThreads();
ConsumeTask::pCount = new Count[ numCount ];
memset( pCount, 0, sizeof(Count) * numCount );
}
};
ConsumeTask ConsumeTask::tasks[numTasks];
ConsumeTask::Count* ConsumeTask::pCount = NULL;
uint32_t ConsumeTask::numCount = 0;
struct CreateTasks : ITaskSet
{
CreateTasks()
{
m_SetSize = numTasks;
}
void ExecuteRange( TaskSetPartition range_, uint32_t threadnum_ ) override
{
for( uint32_t i=range_.start; i <range_.end; ++i )
{
ConsumeTask& task = ConsumeTask::tasks[ i ];
g_TS.AddTaskSetToPipe( &task );
}
}
};
static const int WARMUPS = 5;
static const int RUNS = 10;
static const int REPEATS = RUNS + WARMUPS;
int main(int argc, const char * argv[])
{
uint32_t maxThreads = enki::GetNumHardwareThreads();
double* times = new double[ maxThreads ];
double* stdev = new double[ maxThreads ];
for( uint32_t numThreads = 1; numThreads <= maxThreads; ++numThreads )
{
g_TS.Initialize(numThreads);
double avTime = 0.0;
double avTime2 = 0.0;
uint32_t totalErrors = 0;
for( int run = 0; run< REPEATS; ++run )
{
printf("Run %d.....\n", run);
Timer tParallel;
CreateTasks createTask;
ConsumeTask::Init();
tParallel.Start();
g_TS.AddTaskSetToPipe( &createTask );
g_TS.WaitforAll();
tParallel.Stop();
printf("Parallel Example complete in \t%fms, task rate: %f M tasks/s\n", tParallel.GetTimeMS(), numTasks / tParallel.GetTimeMS() / 1000.0f );
printf("Parallel Example error checking...");
uint32_t numTasksDone = 0;
for( uint32_t check = 0; check < ConsumeTask::numCount; ++check )
{
numTasksDone += ConsumeTask::pCount[check].count;
}
if( numTasksDone != numTasks )
{
printf("\n ERRORS FOUND - %d tasks not done!!!\n", numTasks - numTasksDone );
}
else
{
printf(" no errors found.\n" );
}
if( run >= WARMUPS )
{
avTime2 += tParallel.GetTimeMS() * tParallel.GetTimeMS();
avTime += tParallel.GetTimeMS() / RUNS;
}
}
printf("\nAverage Time for %d Hardware Threads: %fms, rate: %f M tasks/s. %d errors found.\n", numThreads, avTime, numTasks / avTime / 1000.0f, totalErrors );
times[numThreads-1] = avTime;
stdev[numThreads-1] = sqrt(RUNS * avTime2 - (RUNS * avTime)*(RUNS * avTime)) / RUNS;
}
printf("\nHardware Threads, Time, std, MTasks/s, Perf Multiplier\n" );
for( uint32_t numThreads = 1; numThreads <= maxThreads; ++numThreads )
{
printf("%d, %f, %f, %f, %f\n", numThreads, times[numThreads-1], stdev[numThreads-1], numTasks / times[numThreads-1] / 1000.0f, times[0] / times[numThreads-1] );
}
delete[] times;
return 0;
}