summaryrefslogtreecommitdiff
path: root/Homework/cs3460/array_perf/sortutils.cpp
blob: 402f56123a1b38ddd59b5297459ec899d4763660 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "sortutils.hpp"

/**
 * @struct SortResult stores simple information about a sorting time result
 */
struct SortResult
{
    std::string title;
    std::uint64_t duration;
};

/**
 * @param std::string sequentialString: use "Sequential" or "Parallel"
 * @param std::vector<struct SortResult>: the list of results from a sorting run
 * @return void: format all the sorting results according to program requirements
 */
void formatSortResults(std::string sequentialString, const std::vector<struct SortResult>& results)
{
    const std::string timeStr = " Time";
    std::uint32_t longestTitle = static_cast<std::uint32_t>(std::max_element(
                                                                results.begin(),
                                                                results.end(),
                                                                [](const struct SortResult s1, const struct SortResult s2) { return s1.title.length() < s2.title.length(); })
                                                                ->title.length());
    std::cout << sequentialString << std::endl;
    for (auto sortResult : results)
    {
        std::cout << fmt::format("\t{0:<{1}} : {2} ms", sortResult.title + timeStr, longestTitle + timeStr.length(), sortResult.duration)
                  << std::endl;
    }
    std::cout << std::endl;
}

/**
 * @param std::string title: title of the array type used
 * @param std::vector<struct SortResult> sequentialResults: list of results of sequential sorts
 * @param std::vector<struct SortResult> parallelResults: list of results of parallel sorts
 * @return void: format the results and title according to program requirements
 */
void formatArrayTypeSortResults(std::string title, const std::vector<struct SortResult>& sequentialResults, const std::vector<struct SortResult>& parallelResults)
{
    std::cout << fmt::format("  --- {0} Performance ---", title) << std::endl;
    formatSortResults("Sequential", sequentialResults);
    formatSortResults("Parallel", parallelResults);
}

/**
 * @param SourceArray source: source array to copy into->
 * @param int[] dest: the raw array to copy source's elements to
 * @return void: do the copy
 */
void initializeRawArrayFromStdArray(const SourceArray& source, int dest[])
{
    std::copy(std::execution::par, source.begin(), source.end(), dest);
}

/**
 * @param SourceArray data: the source data to convert into a "organ pipe" array per program requirements
 * @return void: do the piping
 */
void organPipeStdArray(SourceArray& data)
{
    float mid = HOW_MANY_ELEMENTS / 2.0;
    std::reverse_copy(std::execution::par, data.begin(), data.begin() + static_cast<std::uint32_t>(std::floor(mid)), data.begin() + static_cast<std::uint32_t>(std::ceil(mid)));
}

/**
 * @param function<void()> func: the function to time
 * @return uint64_t: the milliseconds required to execute func()
 */
std::uint64_t executionMs(std::function<void()> func)
{
    std::chrono::time_point start = std::chrono::steady_clock::now();
    func();
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
}

/**
 * @param SourceArray source: the values to copy into rawArray HOW_MANY_TIMES times
 * @param int[] rawArray: the raw array to benchmark
 * @param bool parallel: whether to use parallel execution
 * @return uint64_t: time (in ms) to sort copied values from source -> rawArray HOW_MANY_TIMES times
 */
std::uint64_t totalTimeToSortRawArrayManyTimes(const SourceArray& source, int rawArray[], bool parallel)
{
    std::uint64_t ms = 0;
    for (std::uint32_t i = 0; i < HOW_MANY_TIMES; i++)
    {
        initializeRawArrayFromStdArray(source, rawArray);
        if (parallel)
        {
            ms += executionMs([&rawArray]() -> void { std::sort(std::execution::par_unseq, rawArray, rawArray + HOW_MANY_ELEMENTS); });
        }
        else
        {
            ms += executionMs([&rawArray]() -> void { std::sort(std::execution::seq, rawArray, rawArray + HOW_MANY_ELEMENTS); });
        }
    }
    return ms;
}

/**
 * @param SourceArray source: the values to copy into rawArray HOW_MANY_TIMES times
 * @param SourceArray stdArray: the std::array to benchmark
 * @param bool parallel: whether to use parallel execution
 * @return uint64_t: time (in ms) to sort copied values from source -> stdArray HOW_MANY_TIMES times
 */
std::uint64_t totalTimeToSortStdArrayManyTimes(const SourceArray& source, SourceArray& stdArray, bool parallel)
{
    std::uint64_t ms = 0;
    for (std::uint32_t i = 0; i < HOW_MANY_TIMES; i++)
    {
        std::copy(source.begin(), source.end(), stdArray.begin());
        if (parallel)
        {
            ms += executionMs([&stdArray]() -> void { std::sort(std::execution::par_unseq, stdArray.begin(), stdArray.end()); });
        }
        else
        {
            ms += executionMs([&stdArray]() -> void { std::sort(std::execution::seq, stdArray.begin(), stdArray.end()); });
        }
    }
    return ms;
}

/**
 * @param SourceArray source: the values to copy into rawArray HOW_MANY_TIMES times
 * @param SourceArray stdVector: the vector to benchmark
 * @param bool parallel: whether to use parallel execution
 * @return uint64_t: time (in ms) to sort copied values from source -> stdVector HOW_MANY_TIMES times
 */
std::uint64_t totalTimeToSortStdVectorManyTimes(const SourceArray& source, std::vector<int>& stdVector, bool parallel)
{
    std::uint64_t ms = 0;
    for (std::uint32_t i = 0; i < HOW_MANY_TIMES; i++)
    {
        std::copy(source.begin(), source.end(), stdVector.begin());
        if (parallel)
        {
            ms += executionMs([&stdVector]() -> void { std::sort(std::execution::par_unseq, stdVector.begin(), stdVector.end()); });
        }
        else
        {
            ms += executionMs([&stdVector]() -> void { std::sort(std::execution::seq, stdVector.begin(), stdVector.end()); });
        }
    }
    return ms;
}

/**
 * @param SourceArray random: a list of random values initialize in main.cpp
 * @param SourceArray sorted: random, but sorted in sequential order
 * @param SourceArray reversed: sorted, but reversed
 * @param SourceArray organPipe: sorted, but "organ-piped"
 * @param SourceArray rotated: sorted, but rotated by one
 * @return void: format results of the raw array benchmark
 */
void evaluateRawArray(const SourceArray& random, const SourceArray& sorted, const SourceArray& reversed, const SourceArray& organPipe, const SourceArray& rotated)
{
    int rawArray[HOW_MANY_ELEMENTS];
    std::vector<struct SortResult> seqResults{
        { "Random", totalTimeToSortRawArrayManyTimes(random, rawArray, false) },
        { "Sorted", totalTimeToSortRawArrayManyTimes(sorted, rawArray, false) },
        { "Reversed", totalTimeToSortRawArrayManyTimes(reversed, rawArray, false) },
        { "Organ Piped", totalTimeToSortRawArrayManyTimes(organPipe, rawArray, false) },
        { "Rotated", totalTimeToSortRawArrayManyTimes(rotated, rawArray, false) },
    };

    std::vector<struct SortResult> parResults{
        { "Random", totalTimeToSortRawArrayManyTimes(random, rawArray, true) },
        { "Sorted", totalTimeToSortRawArrayManyTimes(sorted, rawArray, true) },
        { "Reversed", totalTimeToSortRawArrayManyTimes(reversed, rawArray, true) },
        { "Organ Piped", totalTimeToSortRawArrayManyTimes(organPipe, rawArray, true) },
        { "Rotated", totalTimeToSortRawArrayManyTimes(rotated, rawArray, true) },
    };

    formatArrayTypeSortResults("Raw Array", seqResults, parResults);
}

/**
 * @param SourceArray random: a list of random values initialize in main.cpp
 * @param SourceArray sorted: random, but sorted in sequential order
 * @param SourceArray reversed: sorted, but reversed
 * @param SourceArray organPipe: sorted, but "organ-piped"
 * @param SourceArray rotated: sorted, but rotated by one
 * @return void: format results of the std array benchmark
 */
void evaluateStdArray(const SourceArray& random, const SourceArray& sorted, const SourceArray& reversed, const SourceArray& organPipe, const SourceArray& rotated)
{
    SourceArray stdArray;
    std::vector<struct SortResult> seqResults{
        { "Random", totalTimeToSortStdArrayManyTimes(random, stdArray, false) },
        { "Sorted", totalTimeToSortStdArrayManyTimes(sorted, stdArray, false) },
        { "Reversed", totalTimeToSortStdArrayManyTimes(reversed, stdArray, false) },
        { "Organ Piped", totalTimeToSortStdArrayManyTimes(organPipe, stdArray, false) },
        { "Rotated", totalTimeToSortStdArrayManyTimes(rotated, stdArray, false) },
    };

    std::vector<struct SortResult> parResults{
        { "Random", totalTimeToSortStdArrayManyTimes(random, stdArray, true) },
        { "Sorted", totalTimeToSortStdArrayManyTimes(sorted, stdArray, true) },
        { "Reversed", totalTimeToSortStdArrayManyTimes(reversed, stdArray, true) },
        { "Organ Piped", totalTimeToSortStdArrayManyTimes(organPipe, stdArray, true) },
        { "Rotated", totalTimeToSortStdArrayManyTimes(rotated, stdArray, true) },
    };

    formatArrayTypeSortResults("std::array", seqResults, parResults);
}

/**
 * @param SourceArray random: a list of random values initialize in main.cpp
 * @param SourceArray sorted: random, but sorted in sequential order
 * @param SourceArray reversed: sorted, but reversed
 * @param SourceArray organPipe: sorted, but "organ-piped"
 * @param SourceArray rotated: sorted, but rotated by one
 * @return void: format results of the std vector benchmark
 */
void evaluateStdVector(const SourceArray& random, const SourceArray& sorted, const SourceArray& reversed, const SourceArray& organPipe, const SourceArray& rotated)
{
    std::vector<int> stdVector(HOW_MANY_ELEMENTS);
    std::vector<struct SortResult> seqResults{
        { "Random", totalTimeToSortStdVectorManyTimes(random, stdVector, false) },
        { "Sorted", totalTimeToSortStdVectorManyTimes(sorted, stdVector, false) },
        { "Reversed", totalTimeToSortStdVectorManyTimes(reversed, stdVector, false) },
        { "Organ Piped", totalTimeToSortStdVectorManyTimes(organPipe, stdVector, false) },
        { "Rotated", totalTimeToSortStdVectorManyTimes(rotated, stdVector, false) },
    };

    std::vector<struct SortResult> parResults{
        { "Random", totalTimeToSortStdVectorManyTimes(random, stdVector, true) },
        { "Sorted", totalTimeToSortStdVectorManyTimes(sorted, stdVector, true) },
        { "Reversed", totalTimeToSortStdVectorManyTimes(reversed, stdVector, true) },
        { "Organ Piped", totalTimeToSortStdVectorManyTimes(organPipe, stdVector, true) },
        { "Rotated", totalTimeToSortStdVectorManyTimes(rotated, stdVector, true) },
    };

    formatArrayTypeSortResults("std::vector", seqResults, parResults);
}