-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
enkiTSRemoteryExample.cpp
194 lines (156 loc) · 5.24 KB
/
enkiTSRemoteryExample.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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 <stdio.h>
#include <inttypes.h>
#include <assert.h>
#include <string.h>
#include <sstream>
#include "Remotery.h"
using namespace enki;
TaskScheduler g_TS;
static const int RUNS = 1024*1024;
static const int MINRANGE = 10 * 1024;
static const int SUMS = 10*1024*1024;
struct ParallelSumTaskSet : ITaskSet
{
struct Count
{
// prevent false sharing.
uint64_t count;
char cacheline[64];
};
Count* m_pPartialSums;
uint32_t m_NumPartialSums;
ParallelSumTaskSet( uint32_t size_ ) : m_pPartialSums(NULL), m_NumPartialSums(0) { m_SetSize = size_; m_MinRange = MINRANGE; }
virtual ~ParallelSumTaskSet()
{
delete[] m_pPartialSums;
}
void Init()
{
delete[] m_pPartialSums;
m_NumPartialSums = g_TS.GetNumTaskThreads();
m_pPartialSums = new Count[ m_NumPartialSums ];
memset( m_pPartialSums, 0, sizeof(Count)*m_NumPartialSums );
}
virtual void ExecuteRange( TaskSetPartition range, uint32_t threadnum )
{
rmt_ScopedCPUSample(Sum, 0);
assert( m_pPartialSums && m_NumPartialSums );
uint64_t sum = m_pPartialSums[threadnum].count;
for( uint64_t i = range.start; i < range.end; ++i )
{
sum += i + 1;
}
m_pPartialSums[threadnum].count = sum;
}
};
struct ParallelReductionSumTaskSet : ITaskSet
{
ParallelSumTaskSet m_ParallelSumTaskSet;
uint64_t m_FinalSum;
ParallelReductionSumTaskSet( uint32_t size_ ) : m_ParallelSumTaskSet( size_ ), m_FinalSum(0) {}
void Init()
{
m_ParallelSumTaskSet.Init();
}
virtual void ExecuteRange( TaskSetPartition range, uint32_t threadnum )
{
rmt_ScopedCPUSample(Reduce, 0);
g_TS.AddTaskSetToPipe( &m_ParallelSumTaskSet );
g_TS.WaitforTask( &m_ParallelSumTaskSet );
for( uint32_t i = 0; i < m_ParallelSumTaskSet.m_NumPartialSums; ++i )
{
m_FinalSum += m_ParallelSumTaskSet.m_pPartialSums[i].count;
}
}
};
void threadStartCallback( uint32_t threadnum_ )
{
std::ostringstream out;
out << "enkiTS_" << threadnum_;
rmt_SetCurrentThreadName( out.str().c_str() );
}
void waitForNewTaskSuspendStartCallback( uint32_t threadnum_ )
{
rmt_BeginCPUSample(WaitForNewTaskSuspend, 0);
}
void waitForTaskCompleteStartCallback( uint32_t threadnum_ )
{
rmt_BeginCPUSample(WaitForTaskComplete, 0);
}
void waitForTaskCompleteSuspendStartCallback( uint32_t threadnum_ )
{
rmt_BeginCPUSample(WaitForTaskCompleteSuspend, 0);
}
void stopCallback( uint32_t threadnum_ )
{
rmt_EndCPUSample();
}
int main(int argc, const char * argv[])
{
// the example generates a lot of samples, so increase update rate for profiler
rmt_Settings()->maxNbMessagesPerUpdate = 128 * rmt_Settings()->maxNbMessagesPerUpdate;
Remotery* rmt;
rmt_CreateGlobalInstance(&rmt);
enki::TaskSchedulerConfig tsConfig;
tsConfig.profilerCallbacks.threadStart = threadStartCallback;
tsConfig.profilerCallbacks.waitForNewTaskSuspendStart = waitForNewTaskSuspendStartCallback;
tsConfig.profilerCallbacks.waitForNewTaskSuspendStop = stopCallback;
tsConfig.profilerCallbacks.waitForTaskCompleteStart = waitForTaskCompleteStartCallback;
tsConfig.profilerCallbacks.waitForTaskCompleteStop = stopCallback;
tsConfig.profilerCallbacks.waitForTaskCompleteSuspendStart = waitForTaskCompleteSuspendStartCallback;
tsConfig.profilerCallbacks.waitForTaskCompleteSuspendStop = stopCallback;
g_TS.Initialize( tsConfig );
rmt_SetCurrentThreadName("Main");
double avSpeedUp = 0.0;
for( int run = 0; run< RUNS; ++run )
{
rmt_ScopedCPUSample(Run, 0);
printf("Run %d\t Open Remotery\\vis\\index.html to view profiler\n", run);
ParallelReductionSumTaskSet m_ParallelReductionSumTaskSet( SUMS );
{
rmt_ScopedCPUSample(Parallel, 0);
m_ParallelReductionSumTaskSet.Init();
g_TS.AddTaskSetToPipe(&m_ParallelReductionSumTaskSet);
g_TS.WaitforTask(&m_ParallelReductionSumTaskSet);
}
uint64_t sum = 0;
{
rmt_ScopedCPUSample(Serial, 0);
for (uint64_t i = 0; i < (uint64_t)m_ParallelReductionSumTaskSet.m_ParallelSumTaskSet.m_SetSize; ++i)
{
sum += i + 1;
}
}
printf("Sun Parallel: %" PRIu64 ", sum serial %" PRIu64, m_ParallelReductionSumTaskSet.m_FinalSum, sum );
if( m_ParallelReductionSumTaskSet.m_FinalSum == sum )
{
printf(" - Sums Correct.\n");
}
else
{
assert(false);
printf(" - Sums ERROR.\n");
}
}
g_TS.WaitforAllAndShutdown();
rmt_DestroyGlobalInstance(rmt);
return 0;
}