Skip to content
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
wants to merge 5 commits into
base: ics_2021_tutorial
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(array)
add_subdirectory(edge_index_graph)
add_subdirectory(blackscholes)
add_subdirectory(pi)
add_subdirectory(stl_algorithm)
6 changes: 6 additions & 0 deletions examples/stl_algorithm/CMakeLists.txt
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)
143 changes: 143 additions & 0 deletions examples/stl_algorithm/array.cc
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>;
Copy link
Collaborator

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?

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
145 changes: 145 additions & 0 deletions examples/stl_algorithm/unordered_map.cc
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
Loading