Skip to content

Commit afb6421

Browse files
jonaslattgonzalobg
authored andcommitted
First implementation of the Jupyter notebook done.
1 parent 1574273 commit afb6421

File tree

7 files changed

+737
-4
lines changed

7 files changed

+737
-4
lines changed

labs/lab1_select/exercise0.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2022 University of Geneva. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include <algorithm>
25+
#include <numeric>
26+
#include <vector>
27+
#include <iterator>
28+
#include <iostream>
29+
#if defined(__clang__)
30+
// clang does not support libstdc++ ranges
31+
#include <range/v3/all.hpp>
32+
namespace views = ranges::views;
33+
#elif __cplusplus >= 202002L
34+
#include <ranges>
35+
namespace views = std::views;
36+
#endif
37+
38+
// Initialize vector
39+
void initialize(std::vector<int>& v);
40+
41+
// Select elements and copy them to a new vector
42+
template<class UnaryPredicate>
43+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred);
44+
45+
46+
int main(int argc, char* argv[])
47+
{
48+
// Read CLI arguments, the first argument is the name of the binary:
49+
if (argc != 2) {
50+
std::cerr << "ERROR: Missing length argument!" << std::endl;
51+
return 1;
52+
}
53+
54+
// Read length of vector elements
55+
long long n = std::stoll(argv[1]);
56+
57+
// Allocate the data vector
58+
auto v = std::vector<int>(n);
59+
60+
initialize(v);
61+
62+
auto predicate = [](int x) { return x % 3 == 0; };
63+
auto w = select(v, predicate);
64+
if (!all_of(w, predicate)) {
65+
std::cerr << "ERROR!" << std::endl;
66+
return 1;
67+
}
68+
std::cerr << "OK!" << std::endl;
69+
70+
std::cout << "w = ";
71+
std::copy(w.begin(), w.end(), std::ostream_iterator<int>(std::cout, " "));
72+
std::cout << std::endl;
73+
74+
return 0;
75+
}
76+
77+
void initialize(std::vector<int>& v)
78+
{
79+
auto distribution = std::uniform_int_distribution<int> {0, 100};
80+
auto engine = std::mt19937 {1};
81+
std::generate(v.begin(), v.end(), [&distribution, &engine]{ return distribution(engine); });
82+
}
83+
84+
// This version of "select" can only run sequentially, because the output
85+
// vector w is built consecutively during the traversal of the input vector v.
86+
template<class UnaryPredicate>
87+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred)
88+
{
89+
// TODO Instead of the line below, create a vector w and use a "copy_if" algorithm
90+
// call to copy all elements from v to w that are selected by the unary predicate.
91+
auto w = v;
92+
return w;
93+
}

labs/lab1_select/exercise1.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2022 University of Geneva. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include <algorithm>
25+
#include <numeric>
26+
#include <vector>
27+
#include <iterator>
28+
#include <iostream>
29+
#if defined(__clang__)
30+
// clang does not support libstdc++ ranges
31+
#include <range/v3/all.hpp>
32+
namespace views = ranges::views;
33+
#elif __cplusplus >= 202002L
34+
#include <ranges>
35+
namespace views = std::views;
36+
#endif
37+
38+
// Initialize vector
39+
void initialize(std::vector<int>& v);
40+
41+
// Select elements and copy them to a new vector
42+
template<class UnaryPredicate>
43+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred);
44+
45+
int main(int argc, char* argv[])
46+
{
47+
// Read CLI arguments, the first argument is the name of the binary:
48+
if (argc != 2) {
49+
std::cerr << "ERROR: Missing length argument!" << std::endl;
50+
return 1;
51+
}
52+
53+
// Read length of vector elements
54+
long long n = std::stoll(argv[1]);
55+
56+
// Allocate the data vector
57+
auto v = std::vector<int>(n);
58+
59+
initialize(v);
60+
61+
auto predicate = [](int x) { return x % 3 == 0; };
62+
auto w = select(v, predicate);
63+
if (!all_of(w, predicate)) {
64+
std::cerr << "ERROR!" << std::endl;
65+
return 1;
66+
}
67+
68+
std::cout << "w = ";
69+
std::copy(w.begin(), w.end(), std::ostream_iterator<int>(std::cout, " "));
70+
std::cout << std::endl;
71+
72+
return 0;
73+
}
74+
75+
void initialize(std::vector<int>& v)
76+
{
77+
auto distribution = std::uniform_int_distribution<int> {0, 100};
78+
auto engine = std::mt19937 {1};
79+
std::generate(v.begin(), v.end(), [&distribution, &engine]{ return distribution(engine); });
80+
}
81+
82+
template<class UnaryPredicate>
83+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred)
84+
{
85+
// TODO: Allow this version of the code to run in parallel, proceeding in three steps:
86+
std::vector<char> v_sel(v.size());
87+
// 1. Fill v_sel with 0/1 values, depending on the outcome of the unary predicatea.
88+
89+
std::vector<size_t> index(v.size());
90+
// 2. Compute the cumulative sum of v_sel using inclusive_scan.
91+
92+
size_t numElem = index.empty() ? 0 : index.back();
93+
std::vector<int> w(numElem);
94+
// 3. Use for_each to copy the selected elements from v to w.
95+
96+
return w;
97+
}

labs/lab1_select/exercise2.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2022 University of Geneva. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include <algorithm>
25+
#include <numeric>
26+
#include <vector>
27+
#include <iterator>
28+
#include <iostream>
29+
#if defined(__clang__)
30+
// clang does not support libstdc++ ranges
31+
#include <range/v3/all.hpp>
32+
namespace views = ranges::views;
33+
#elif __cplusplus >= 202002L
34+
#include <ranges>
35+
namespace views = std::views;
36+
#endif
37+
38+
// Initialize vector
39+
void initialize(std::vector<int>& v);
40+
41+
// Select elements and copy them to a new vector
42+
template<class UnaryPredicate>
43+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred);
44+
45+
int main(int argc, char* argv[])
46+
{
47+
// Read CLI arguments, the first argument is the name of the binary:
48+
if (argc != 2) {
49+
std::cerr << "ERROR: Missing length argument!" << std::endl;
50+
return 1;
51+
}
52+
53+
// Read length of vector elements
54+
long long n = std::stoll(argv[1]);
55+
56+
// Allocate the data vector
57+
auto v = std::vector<int>(n);
58+
59+
initialize(v);
60+
61+
auto predicate = [](int x) { return x % 3 == 0; };
62+
auto w = select(v, predicate);
63+
if (!all_of(w, predicate)) {
64+
std::cerr << "ERROR!" << std::endl;
65+
return 1;
66+
}
67+
std::cerr << "OK!" << std::endl;
68+
69+
std::cout << "w = ";
70+
std::copy(w.begin(), w.end(), std::ostream_iterator<int>(std::cout, " "));
71+
std::cout << std::endl;
72+
73+
return 0;
74+
}
75+
76+
void initialize(std::vector<int>& v)
77+
{
78+
auto distribution = std::uniform_int_distribution<int> {0, 100};
79+
auto engine = std::mt19937 {1};
80+
std::generate(v.begin(), v.end(), [&distribution, &engine]{ return distribution(engine); });
81+
}
82+
83+
template<class UnaryPredicate>
84+
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred)
85+
{
86+
// TODO: write a parallelizable version of select, just as for exercise 1.
87+
// But this time, use transform_inclusive_scan to reduce the number of steps from 3 to 2.
88+
89+
return w;
90+
}
91+

0 commit comments

Comments
 (0)