-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#198] shad STL algorithm examples #199
Open
NanmiaoWu
wants to merge
5
commits into
pnnl:ics_2021_tutorial
Choose a base branch
from
STEllAR-GROUP:tutorial_using_gmt
base: ics_2021_tutorial
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
24efa99
adding tutorial for shad array, unordered_set, unordered_map using gm…
NanmiaoWu a066e04
[#198] add shad STL algorithm examples
NanmiaoWu cb2dbf3
adding tutorial for shad array, unordered_set, unordered_map using gm…
NanmiaoWu 6e3c409
[#198] add shad STL algorithm examples
NanmiaoWu 3705490
[#198] using shad::array::iterator; fix build errors on ci
NanmiaoWu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
set(program array unordered_set unordered_map) | ||
|
||
foreach(p ${program}) | ||
add_executable(${p} ${p}.cc) | ||
target_link_libraries(${p} ${SHAD_RUNTIME_LIB} runtime) | ||
endforeach(p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//===------------------------------------------------------------*- C++ -*-===// | ||
// | ||
// SHAD | ||
// | ||
// The Scalable High-performance Algorithms and Data Structure Library | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Copyright 2018 Battelle Memorial Institute | ||
// | ||
// 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. | ||
// | ||
//===----------------------------------------------------------------------===/ | ||
|
||
#include <stdlib.h> | ||
#include <iostream> | ||
#include <random> | ||
|
||
#include "shad/core/algorithm.h" | ||
#include "shad/core/array.h" | ||
#include "shad/util/measure.h" | ||
|
||
constexpr static size_t kArraySize = 1024; | ||
using array_t = shad::impl::array<int, kArraySize>; | ||
using iterator = array_t::iterator; | ||
|
||
void shad_generate_algorithm(shad::array<int, kArraySize> &in) { | ||
shad::generate(shad::distributed_parallel_tag{}, in.begin(), in.end(), [=]() { | ||
std::random_device rd; | ||
std::default_random_engine G(rd()); | ||
std::uniform_int_distribution<int> dist(1, 10); | ||
return dist(G); | ||
}); | ||
} | ||
|
||
void shad_fill_algorithm(shad::array<int, kArraySize> &in) { | ||
shad::fill(shad::distributed_parallel_tag{}, in.begin(), in.end(), 42); | ||
} | ||
|
||
size_t shad_count_algorithm(shad::array<int, kArraySize> &in) { | ||
auto counter = | ||
shad::count(shad::distributed_parallel_tag{}, in.begin(), in.end(), 1); | ||
return counter; | ||
} | ||
|
||
iterator shad_find_if_algorithm(shad::array<int, kArraySize> &in) { | ||
auto iter = shad::find_if(shad::distributed_parallel_tag{}, in.begin(), | ||
in.end(), [](int i) { return i % 2 == 0; }); | ||
return iter; | ||
} | ||
|
||
void shad_for_each_algorithm(shad::array<int, kArraySize> &in) { | ||
shad::for_each(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
[](int &i) { i++; }); | ||
} | ||
|
||
std::pair<iterator, iterator> shad_minmax_algorithm( | ||
shad::array<int, kArraySize> &in) { | ||
auto res = shad::minmax_element(shad::distributed_parallel_tag{}, in.begin(), | ||
in.end()); | ||
return res; | ||
} | ||
|
||
void shad_transform_algorithm(shad::array<int, kArraySize> &in) { | ||
shad::transform(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
in.begin(), [](int i) { return i + 2; }); | ||
} | ||
|
||
namespace shad { | ||
|
||
int main(int argc, char *argv[]) { | ||
// array | ||
shad::array<int, kArraySize> in; | ||
|
||
// shad fill algorithm | ||
auto execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { shad_fill_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::fill took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
// shad generate algorithm | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { shad_generate_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::generate took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
// shad count algorithm | ||
size_t counter; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { counter = shad_count_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::count took " << execute_time.count() | ||
<< " seconds (numbers of 0 = " << counter << " )" << std::endl; | ||
|
||
// shad find_if algorithm | ||
iterator iter; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { iter = shad_find_if_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::find_if took " << execute_time.count() | ||
<< " seconds, "; | ||
(iter != in.end()) | ||
? std::cout << "array contains an even number" << std::endl | ||
: std::cout << "array does not contain even numbers" << std::endl; | ||
|
||
// shad for_each algorithm | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { shad_for_each_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::for_each took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
// shad minmax algorithm | ||
std::pair<iterator, iterator> min_max; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { min_max = shad_minmax_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::minmax took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
// shad transform algorithm | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { shad_transform_algorithm(in); }); | ||
std::cout << "Array, using " << shad::rt::numLocalities() | ||
<< " localities, shad::transform took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace shad |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===------------------------------------------------------------*- C++ -*-===// | ||
// | ||
// SHAD | ||
// | ||
// The Scalable High-performance Algorithms and Data Structure Library | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Copyright 2018 Battelle Memorial Institute | ||
// | ||
// 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. | ||
// | ||
//===----------------------------------------------------------------------===/ | ||
|
||
#include <iostream> | ||
|
||
#include "shad/core/algorithm.h" | ||
#include "shad/core/unordered_map.h" | ||
#include "shad/util/measure.h" | ||
|
||
constexpr static size_t kSize = 1024; | ||
using hashmap_t = | ||
shad::Hashmap<int, int, shad::MemCmp<int>, shad::Updater<int>>; | ||
using iterator = hashmap_t::iterator; | ||
using value_type = hashmap_t::value_type; | ||
using shad_inserter_t = shad::insert_iterator<shad::unordered_map<int, int>>; | ||
using shad_buffered_inserter_t = | ||
shad::buffered_insert_iterator<shad::unordered_map<int, int>>; | ||
|
||
std::pair<iterator, iterator> shad_minmax_algorithm( | ||
shad::unordered_map<int, int> &in) { | ||
auto res = shad::minmax_element(shad::distributed_parallel_tag{}, in.begin(), | ||
in.end()); | ||
return res; | ||
} | ||
|
||
iterator shad_find_if_algorithm(shad::unordered_map<int, int> &in) { | ||
auto iter = | ||
shad::find_if(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
[](const value_type &i) { return i.second % 2 == 0; }); | ||
return iter; | ||
} | ||
|
||
bool shad_any_of_algorithm(shad::unordered_map<int, int> &in) { | ||
auto res = | ||
shad::any_of(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
[](const value_type &i) { return i.second % 7 == 0; }); | ||
return res; | ||
} | ||
|
||
size_t shad_count_if_algorithm(shad::unordered_map<int, int> &in) { | ||
auto counter = | ||
shad::count_if(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
[](const value_type &i) { return i.second % 4 == 0; }); | ||
return counter; | ||
} | ||
|
||
template <typename shad_inserter> | ||
void shad_transform_algorithm(shad::unordered_map<int, int> &in) { | ||
shad::unordered_map<int, int> out; | ||
shad::transform(shad::distributed_parallel_tag{}, in.begin(), in.end(), | ||
shad_inserter(out, out.begin()), | ||
[](const value_type &i) { return i; }); | ||
} | ||
|
||
namespace shad { | ||
|
||
int main(int argc, char *argv[]) { | ||
// unordered_map | ||
shad::unordered_map<int, int> map_; | ||
|
||
// create map | ||
shad_buffered_inserter_t ins(map_, map_.begin()); | ||
for (size_t i = 0; i < kSize; ++i) { | ||
ins = std::make_pair(i, 3 * (i + 1)); | ||
} | ||
ins.wait(); | ||
ins.flush(); | ||
|
||
// shad minmax algorithm | ||
std::pair<iterator, iterator> min_max; | ||
auto execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { min_max = shad_minmax_algorithm(map_); }); | ||
std::cout << "Unordered map, using " << shad::rt::numLocalities() | ||
<< " localities, shad::count took " << execute_time.count() | ||
<< " seconds (min = " << (*min_max.first).second | ||
<< ", max = " << (*min_max.second).second << " )" << std::endl; | ||
|
||
// shad find_if algorithm | ||
iterator iter; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { iter = shad_find_if_algorithm(map_); }); | ||
std::cout << "Unordered map, using " << shad::rt::numLocalities() | ||
<< " localities, shad::find_if took " << execute_time.count() | ||
<< " seconds, "; | ||
(iter != map_.end()) | ||
? std::cout << "and this unordered map contains an even number" | ||
<< std::endl | ||
: std::cout << "and this unordered map does not contain even numbers" | ||
<< std::endl; | ||
|
||
// shad any_of algorithm | ||
bool res; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { res = shad_any_of_algorithm(map_); }); | ||
std::cout << "Unordered map, using " << shad::rt::numLocalities() | ||
<< " localities, shad::any_of took " << execute_time.count() | ||
<< " seconds, "; | ||
(res == true) ? std::cout << "and this unordered map contains at least one " | ||
"number that is divisible by 7" | ||
<< std::endl | ||
: std::cout << "and this unordered map does not contain any " | ||
"number that is divisible by 7" | ||
<< std::endl; | ||
|
||
// shad count_if algorithm | ||
size_t counter; | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { counter = shad_count_if_algorithm(map_); }); | ||
std::cout << "Unordered map, using " << shad::rt::numLocalities() | ||
<< " localities, shad::count_if took " << execute_time.count() | ||
<< " seconds, " | ||
<< "and number divisible by 4: " << counter << std::endl; | ||
|
||
// shad transform algorithm | ||
execute_time = shad::measure<std::chrono::seconds>::duration( | ||
[&]() { shad_transform_algorithm<shad_inserter_t>(map_); }); | ||
std::cout << "Unordered map, using " << shad::rt::numLocalities() | ||
<< " localities, shad::transform took " << execute_time.count() | ||
<< " seconds" << std::endl; | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace shad |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why using the impl::array?