Skip to content

Commit e53a159

Browse files
author
John Farrier
committed
Merge pull request #54 from DigitalInBlue/develop
Added Multithreading
2 parents ba45d10 + 325ef7e commit e53a159

22 files changed

+799
-101
lines changed

CMakeLists.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ endif()
9292
if(UNIX)
9393
if(CMAKE_COMPILER_IS_GNUCXX)
9494
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -std=gnu++11 -Wall -O3" CACHE STRING "Compiler Flags for All Builds" FORCE)
95-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic" CACHE STRING "Compiler Flags for Debug Builds" FORCE)
95+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic -O0 -g" CACHE STRING "Compiler Flags for Debug Builds" FORCE)
9696
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Compiler Flags for Release Builds" FORCE)
9797
else()
9898
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -std=gnu++11 -Wall -fPIC -O3" CACHE STRING "Compiler Flags for All Builds" FORCE)
99-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic" CACHE STRING "Compiler Flags for Debug Builds" FORCE)
99+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic -O0 -g" CACHE STRING "Compiler Flags for Debug Builds" FORCE)
100100
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "Compiler Flags for Release Builds" FORCE)
101101
endif()
102102
endif()
@@ -163,6 +163,8 @@ set(TARGET_H
163163
include/celero/ResultTable.h
164164
include/celero/Statistics.h
165165
include/celero/TestFixture.h
166+
include/celero/ThreadLocal.h
167+
include/celero/ThreadTestFixture.h
166168
include/celero/TestVector.h
167169
include/celero/Timer.h
168170
include/celero/Utilities.h
@@ -184,12 +186,23 @@ set(TARGET_SRC
184186
src/Statistics.cpp
185187
src/TestVector.cpp
186188
src/TestFixture.cpp
189+
src/ThreadTestFixture.cpp
187190
src/Timer.cpp
188191
src/Utilities.cpp
189192
)
190193

191-
set(TARGET_LIBRARIES ${SYSLIBS})
194+
if(MSVC)
195+
set(SYSLIBS
196+
)
197+
else()
198+
#pthread is required for std::thread to work.
199+
set(SYSLIBS
200+
pthread
201+
)
202+
endif()
203+
192204
add_library(${PROJECT_NAME} ${CELERO_USER_DEFINED_SHARED_OR_STATIC} ${TARGET_SRC} ${TARGET_H})
205+
target_link_libraries(${PROJECT_NAME} ${SYSLIBS})
193206
include_directories(${HEADER_PATH})
194207

195208
install(TARGETS ${PROJECT_NAME}
@@ -229,7 +242,7 @@ if(CELERO_ENABLE_TESTS)
229242
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER "Celero/Test")
230243
endif()
231244
endif()
232-
245+
233246
# ---------------------------------------------------------------------------
234247
# Optional
235248
# ---------------------------------------------------------------------------
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#
2+
# Standard Celero Experiment CMake File
3+
#
4+
# Copyright 2015 John Farrier
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
#
20+
# CMake Configuration
21+
#
22+
23+
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
24+
25+
#
26+
# Set the experiment's name here. Start it with "celero" and use camel case naming.
27+
#
28+
SET(PROJECT_EXPERIMENT_NAME celeroDemoFileWrite)
29+
30+
# Broiler Plate: Set up a CMake option.
31+
option(CELEROExperiment_${PROJECT_EXPERIMENT_NAME} "Set to ON to build ${PROJECT_EXPERIMENT_NAME}." ON)
32+
33+
# Broiler Plate: Test the CMake option.
34+
if(CELEROExperiment_${PROJECT_EXPERIMENT_NAME})
35+
#
36+
# Add Header Files
37+
#
38+
set(TARGET_H
39+
)
40+
41+
#
42+
# Add Sources
43+
#
44+
set(TARGET_SRC
45+
DemoFileWrite.cpp
46+
)
47+
48+
# Broiler Plate: Assign the src and headers to the executable.
49+
add_executable(${PROJECT_EXPERIMENT_NAME}
50+
${TARGET_SRC}
51+
${TARGET_H}
52+
)
53+
54+
# Broiler Plate: Celero Project Dependencies
55+
add_dependencies(${PROJECT_EXPERIMENT_NAME} celero)
56+
target_link_libraries(${PROJECT_EXPERIMENT_NAME} celero)
57+
58+
# Broiler Plate: VS2012 doesn't support true variadic templates
59+
if(MSVC)
60+
add_definitions( /D _VARIADIC_MAX=10 )
61+
endif()
62+
63+
# Broiler Plate: Add Celer's include directories.
64+
include_directories(${HEADER_PATH})
65+
66+
# Broiler Plate: Show here how to automate running after a build (comment in if desired)
67+
# if(CELERO_RUN_EXAMPLE_ON_BUILD)
68+
# add_custom_command(TARGET ${PROJECT_EXPERIMENT_NAME} POST_BUILD COMMAND $<TARGET_FILE:${PROJECT_EXPERIMENT_NAME}>)
69+
# endif()
70+
71+
# Broiler Plate: Set up folders for an IDE.
72+
if(CELERO_ENABLE_FOLDERS)
73+
set_property(TARGET ${PROJECT_EXPERIMENT_NAME} PROPERTY FOLDER "Celero/Experiments")
74+
endif()
75+
76+
endif(CELEROExperiment_${PROJECT_EXPERIMENT_NAME})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <celero/Celero.h>
2+
3+
CELERO_MAIN
4+
5+
class BaseFixture : public celero::TestFixture
6+
{
7+
public:
8+
std::vector<int64_t> getExperimentValues() const override
9+
{
10+
std::vector<int64_t> bufferSizes;
11+
bufferSizes.push_back(32);
12+
bufferSizes.push_back(64);
13+
bufferSizes.push_back(128);
14+
bufferSizes.push_back(256);
15+
bufferSizes.push_back(512);
16+
bufferSizes.push_back(1024);
17+
bufferSizes.push_back(2048);
18+
bufferSizes.push_back(4096);
19+
return bufferSizes;
20+
}
21+
22+
// Scale the experiment values to megabytes.
23+
double getExperimentValueResultScale() const override
24+
{
25+
return 1024.0 * 1024.0;
26+
}
27+
28+
void setUp(int64_t experimentValue) override
29+
{
30+
for(auto i = 0; i < experimentValue; ++i)
31+
{
32+
this->buffer.push_back(rand() % 256);
33+
}
34+
}
35+
36+
void tearDown() override
37+
{
38+
this->buffer.clear();
39+
}
40+
41+
std::vector<char> buffer;
42+
};
43+
44+
class StdFileFixture : public BaseFixture
45+
{
46+
public:
47+
void setUp(int64_t experimentValue) override
48+
{
49+
BaseFixture::setUp(experimentValue);
50+
this->file = fopen("FileWrite.out", "wb");
51+
}
52+
53+
void tearDown() override
54+
{
55+
fclose(this->file);
56+
BaseFixture::tearDown();
57+
}
58+
59+
FILE* file;
60+
};
61+
62+
BASELINE_F(FileWrite, fwrite, StdFileFixture, 30, 5000)
63+
{
64+
fwrite(buffer.data(), sizeof(char), buffer.size() * sizeof(char), file);
65+
}
66+
67+
BENCHMARK_F(FileWrite, fwrite_with_fflush, StdFileFixture, 30, 5000)
68+
{
69+
fwrite(buffer.data(), sizeof(char), buffer.size() * sizeof(char), file);
70+
fflush(file);
71+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#
2+
# Standard Celero Experiment CMake File
3+
#
4+
# Copyright 2015 John Farrier
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
#
20+
# CMake Configuration
21+
#
22+
23+
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
24+
25+
#
26+
# Set the experiment's name here. Start it with "celero" and use camel case naming.
27+
#
28+
SET(PROJECT_EXPERIMENT_NAME celeroDemoMultithread)
29+
30+
# Broiler Plate: Set up a CMake option.
31+
option(CELEROExperiment_${PROJECT_EXPERIMENT_NAME} "Set to ON to build ${PROJECT_EXPERIMENT_NAME}." ON)
32+
33+
# Broiler Plate: Test the CMake option.
34+
if(CELEROExperiment_${PROJECT_EXPERIMENT_NAME})
35+
#
36+
# Add Header Files
37+
#
38+
set(TARGET_H
39+
)
40+
41+
#
42+
# Add Sources
43+
#
44+
set(TARGET_SRC
45+
DemoMultithread.cpp
46+
)
47+
48+
# Broiler Plate: Assign the src and headers to the executable.
49+
add_executable(${PROJECT_EXPERIMENT_NAME}
50+
${TARGET_SRC}
51+
${TARGET_H}
52+
)
53+
54+
# Broiler Plate: Celero Project Dependencies
55+
add_dependencies(${PROJECT_EXPERIMENT_NAME} celero)
56+
target_link_libraries(${PROJECT_EXPERIMENT_NAME} celero)
57+
58+
# Broiler Plate: VS2012 doesn't support true variadic templates
59+
if(MSVC)
60+
add_definitions( /D _VARIADIC_MAX=10 )
61+
endif()
62+
63+
# Broiler Plate: Add Celer's include directories.
64+
include_directories(${HEADER_PATH})
65+
66+
# Broiler Plate: Show here how to automate running after a build (comment in if desired)
67+
# if(CELERO_RUN_EXAMPLE_ON_BUILD)
68+
# add_custom_command(TARGET ${PROJECT_EXPERIMENT_NAME} POST_BUILD COMMAND $<TARGET_FILE:${PROJECT_EXPERIMENT_NAME}>)
69+
# endif()
70+
71+
# Broiler Plate: Set up folders for an IDE.
72+
if(CELERO_ENABLE_FOLDERS)
73+
set_property(TARGET ${PROJECT_EXPERIMENT_NAME} PROPERTY FOLDER "Celero/Experiments")
74+
endif()
75+
76+
endif(CELEROExperiment_${PROJECT_EXPERIMENT_NAME})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <celero/Celero.h>
2+
3+
#include <atomic>
4+
#include <mutex>
5+
6+
CELERO_MAIN
7+
8+
class BaseFixture : public celero::ThreadTestFixture
9+
{
10+
public:
11+
int counter;
12+
13+
void setUp(int64_t) override
14+
{
15+
this->counter = 0;
16+
}
17+
};
18+
19+
class AtomicFixture : public celero::ThreadTestFixture
20+
{
21+
public:
22+
std::atomic<int> counter;
23+
24+
void setUp(int64_t) override
25+
{
26+
this->counter = 0;
27+
}
28+
};
29+
30+
class MutexFixture : public celero::ThreadTestFixture
31+
{
32+
public:
33+
int counter;
34+
std::mutex mutex;
35+
36+
void setUp(int64_t) override
37+
{
38+
this->counter = 0;
39+
}
40+
};
41+
42+
BASELINE_T(DemoMultithread, Baseline, BaseFixture, 10, 10000000, 8)
43+
{
44+
++counter;
45+
}
46+
47+
BENCHMARK_T(DemoMultithread, Atomic, AtomicFixture, 10, 10000000, 8)
48+
{
49+
++counter;
50+
}
51+
52+
BENCHMARK_T(DemoMultithread, Mutex, MutexFixture, 10, 10000000, 8)
53+
{
54+
std::lock_guard<std::mutex> lock(mutex);
55+
++counter;
56+
}

experiments/ExperimentSortingRandomInts/ExperimentSortingRandomInts.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CELERO_MAIN
3636
class SortFixture : public celero::TestFixture
3737
{
3838
public:
39-
SortFixture()
39+
SortFixture()
4040
{
4141
}
4242

@@ -112,6 +112,34 @@ BENCHMARK_F(SortRandInts, SelectionSort, SortFixture, 30, 10000)
112112
}
113113
}
114114

115+
// http://www.bfilipek.com/2014/12/top-5-beautiful-c-std-algorithms.html
116+
BENCHMARK_F(SortRandInts, InsertionSort, SortFixture, 30, 10000)
117+
{
118+
for(auto i = std::begin(this->array); i != std::end(this->array); ++i)
119+
{
120+
std::rotate(std::upper_bound(std::begin(this->array), i, *i), i, std::next(i));
121+
}
122+
}
123+
124+
// http://www.bfilipek.com/2014/12/top-5-beautiful-c-std-algorithms.html
125+
template<class FwdIt, class Compare = std::less<>>
126+
void quickSort(FwdIt first, FwdIt last, Compare cmp = Compare {})
127+
{
128+
auto const N = std::distance(first, last);
129+
if(N <= 1) return;
130+
auto const pivot = std::next(first, N / 2);
131+
std::nth_element(first, pivot, last, cmp);
132+
quickSort(first, pivot, cmp);
133+
quickSort(pivot, last, cmp);
134+
135+
}
136+
137+
BENCHMARK_F(SortRandInts, QuickSort, SortFixture, 30, 10000)
138+
{
139+
quickSort(std::begin(this->array), std::end(this->array));
140+
}
141+
142+
115143
BENCHMARK_F(SortRandInts, stdSort, SortFixture, 30, 10000)
116144
{
117145
std::sort(this->array.begin(), this->array.end());

include/celero/Callbacks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
///
2727
/// Special thanks to the band "3" for providing the development soundtrack.
2828
///
29-
/// "Iterations" refers to how many loops of the test function are used for a single measurement (i.e. per sample).
29+
/// "Iterations" refers to how many loops of the test function are measured as a time.
3030
/// For very fast code, many iterations would help amoratize measurement error.
3131
///
32-
/// "Samples" refers to how many sets of "Ops" will be performed. Each "sample" is
32+
/// "Samples" refers to how many sets of "iterations" will be performed. Each "sample" is
3333
/// a single measurement. Set to 0 to have Celero decide how many samples are required
3434
/// for a minimally significant answer.
3535
///

0 commit comments

Comments
 (0)