File: square.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (277 lines) | stat: -rw-r--r-- 9,755 bytes parent folder | download | duplicates (6)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
    Copyright (c) 2005-2021 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

//
// Example program that reads a file of decimal integers in text format
// and changes each to its square.
//

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>

#include "oneapi/tbb/parallel_pipeline.h"
#include "oneapi/tbb/tick_count.h"
#include "oneapi/tbb/tbb_allocator.h"
#include "oneapi/tbb/global_control.h"

#include "common/utility/utility.hpp"
#include "common/utility/get_default_num_threads.hpp"

extern void generate_if_needed(const char*);

//! Holds a slice of text.
/** Instances *must* be allocated/freed using methods herein, because the C++ declaration
    represents only the header of a much larger object in memory. */
class TextSlice {
    //! Pointer to one past last character in sequence
    char* logical_end;
    //! Pointer to one past last available byte in sequence.
    char* physical_end;

public:
    //! Allocate a TextSlice object that can hold up to max_size characters.
    static TextSlice* allocate(std::size_t max_size) {
        // +1 leaves room for a terminating null character.
        TextSlice* t = (TextSlice*)oneapi::tbb::tbb_allocator<char>().allocate(sizeof(TextSlice) +
                                                                               max_size + 1);
        t->logical_end = t->begin();
        t->physical_end = t->begin() + max_size;
        return t;
    }
    //! Free a TextSlice object
    void free() {
        oneapi::tbb::tbb_allocator<char>().deallocate(
            (char*)this, sizeof(TextSlice) + (physical_end - begin()) + 1);
    }
    //! Pointer to beginning of sequence
    char* begin() {
        return (char*)(this + 1);
    }
    //! Pointer to one past last character in sequence
    char* end() {
        return logical_end;
    }
    //! Length of sequence
    std::size_t size() const {
        return logical_end - (char*)(this + 1);
    }
    //! Maximum number of characters that can be appended to sequence
    std::size_t avail() const {
        return physical_end - logical_end;
    }
    //! Append sequence [first,last) to this sequence.
    void append(char* first, char* last) {
        memcpy(logical_end, first, last - first);
        logical_end += last - first;
    }
    //! Set end() to given value.
    void set_end(char* p) {
        logical_end = p;
    }
};

std::size_t MAX_CHAR_PER_INPUT_SLICE = 4000;
std::string InputFileName = "input.txt";
std::string OutputFileName = "output.txt";

TextSlice* next_slice = nullptr;

class MyInputFunc {
public:
    MyInputFunc(FILE* input_file_);
    MyInputFunc(const MyInputFunc& f) : input_file(f.input_file) {}
    ~MyInputFunc();
    TextSlice* operator()(oneapi::tbb::flow_control& fc) const;

private:
    FILE* input_file;
};

MyInputFunc::MyInputFunc(FILE* input_file_) : input_file(input_file_) {}

MyInputFunc::~MyInputFunc() {}

TextSlice* MyInputFunc::operator()(oneapi::tbb::flow_control& fc) const {
    // Read characters into space that is available in the next slice.
    if (!next_slice)
        next_slice = TextSlice::allocate(MAX_CHAR_PER_INPUT_SLICE);
    std::size_t m = next_slice->avail();
    std::size_t n = fread(next_slice->end(), 1, m, input_file);
    if (!n && next_slice->size() == 0) {
        // No more characters to process
        fc.stop();
        return nullptr;
    }
    else {
        // Have more characters to process.
        TextSlice* t = next_slice;
        next_slice = TextSlice::allocate(MAX_CHAR_PER_INPUT_SLICE);
        char* p = t->end() + n;
        if (n == m) {
            // Might have read partial number.
            // If so, transfer characters of partial number to next slice.
            while (p > t->begin() && isdigit(p[-1]))
                --p;
            assert(p > t->begin()); // Number too large to fit in buffer
            next_slice->append(p, t->end() + n);
        }
        t->set_end(p);
        return t;
    }
}

// Functor that changes each decimal number to its square.
class MyTransformFunc {
public:
    TextSlice* operator()(TextSlice* input) const;
};

TextSlice* MyTransformFunc::operator()(TextSlice* input) const {
    // Add terminating null so that strtol works right even if number is at end of the input.
    *input->end() = '\0';
    char* p = input->begin();
    TextSlice* out = TextSlice::allocate(2 * MAX_CHAR_PER_INPUT_SLICE);
    char* q = out->begin();
    for (;;) {
        while (p < input->end() && !isdigit(*p))
            *q++ = *p++;
        if (p == input->end())
            break;
        long x = strtol(p, &p, 10);
        // Note: no overflow checking is needed here, as we have twice the
        // input string length, but the square of a non-negative integer n
        // cannot have more than twice as many digits as n.
        long y = x * x;
        sprintf(q, "%ld", y);
        q = strchr(q, 0);
    }
    out->set_end(q);
    input->free();
    return out;
}

// Functor that writes a TextSlice to a file.
class MyOutputFunc {
    FILE* my_output_file;

public:
    MyOutputFunc(FILE* output_file);
    void operator()(TextSlice* item) const;
};

MyOutputFunc::MyOutputFunc(FILE* output_file) : my_output_file(output_file) {}

void MyOutputFunc::operator()(TextSlice* out) const {
    std::size_t n = fwrite(out->begin(), 1, out->size(), my_output_file);
    if (n != out->size()) {
        fprintf(stderr, "Can't write into file '%s'\n", OutputFileName.c_str());
        std::exit(-1);
    }
    out->free();
}

bool silent = false;

int run_pipeline(int nthreads) {
    FILE* input_file = fopen(InputFileName.c_str(), "r");
    if (!input_file) {
        throw std::invalid_argument(("Invalid input file name: " + InputFileName).c_str());
        return 0;
    }
    FILE* output_file = fopen(OutputFileName.c_str(), "w");
    if (!output_file) {
        throw std::invalid_argument(("Invalid output file name: " + OutputFileName).c_str());
        return 0;
    }

    oneapi::tbb::tick_count t0 = oneapi::tbb::tick_count::now();

    // Need more than one token in flight per thread to keep all threads
    // busy; 2-4 works
    oneapi::tbb::parallel_pipeline(
        nthreads * 4,
        oneapi::tbb::make_filter<void, TextSlice*>(oneapi::tbb::filter_mode::serial_in_order,
                                                   MyInputFunc(input_file)) &
            oneapi::tbb::make_filter<TextSlice*, TextSlice*>(oneapi::tbb::filter_mode::parallel,
                                                             MyTransformFunc()) &
            oneapi::tbb::make_filter<TextSlice*, void>(oneapi::tbb::filter_mode::serial_in_order,
                                                       MyOutputFunc(output_file)));

    oneapi::tbb::tick_count t1 = oneapi::tbb::tick_count::now();

    fclose(output_file);
    fclose(input_file);

    if (!silent)
        printf("time = %g\n", (t1 - t0).seconds());

    return 1;
}

int main(int argc, char* argv[]) {
    oneapi::tbb::tick_count mainStartTime = oneapi::tbb::tick_count::now();

    // The 1st argument is the function to obtain 'auto' value; the 2nd is the default value
    // The example interprets 0 threads as "run serially, then fully subscribed"
    utility::thread_number_range threads(utility::get_default_num_threads, 0);

    utility::parse_cli_arguments(
        argc,
        argv,
        utility::cli_argument_pack()
            //"-h" option for displaying help is present implicitly
            .positional_arg(threads, "n-of-threads", utility::thread_number_range_desc)
            .positional_arg(InputFileName, "input-file", "input file name")
            .positional_arg(OutputFileName, "output-file", "output file name")
            .positional_arg(MAX_CHAR_PER_INPUT_SLICE,
                            "max-slice-size",
                            "the maximum number of characters in one slice")
            .arg(silent, "silent", "no output except elapsed time"));
    generate_if_needed(InputFileName.c_str());

    if (threads.first) {
        for (int p = threads.first; p <= threads.last; p = threads.step(p)) {
            if (!silent)
                printf("threads = %d ", p);
            oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, p);
            if (!run_pipeline(p))
                return -1;
        }
    }
    else { // Number of threads wasn't set explicitly. Run serial and parallel version
        { // serial run
            if (!silent)
                printf("serial run   ");
            oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 1);
            if (!run_pipeline(1))
                return -1;
        }
        { // parallel run (number of threads is selected automatically)
            if (!silent)
                printf("parallel run ");
            oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism,
                                          utility::get_default_num_threads());
            if (!run_pipeline(utility::get_default_num_threads()))
                return -1;
        }
    }

    utility::report_elapsed_time((oneapi::tbb::tick_count::now() - mainStartTime).seconds());

    return 0;
}