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

Added Sierpinski Triangle example #2012

Merged
merged 7 commits into from Mar 6, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/quickstart/CMakeLists.txt
Expand Up @@ -24,6 +24,7 @@ set(example_programs
fractals
fractals_struct
safe_object
sierpinski
interest_calculator
interval_timer
latch_local
Expand Down Expand Up @@ -53,6 +54,7 @@ set(event_synchronization_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(error_handling_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(fractals_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(interval_timer_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(sierpinski_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(partitioned_vector_spmd_foreach_FLAGS
COMPONENT_DEPENDENCIES iostreams partitioned_vector)
set(pingpong_FLAGS COMPONENT_DEPENDENCIES iostreams)
Expand Down
122 changes: 122 additions & 0 deletions examples/quickstart/CMakeLists.txt~
@@ -0,0 +1,122 @@
# Copyright (c) 2007-2015 Hartmut Kaiser
# Copyright (c) 2011 Bryce Adelstein-Lelbach
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(example_programs
allow_unknown_options
component_ctors
component_in_executable
component_inheritance
component_with_executor
composable_guard
customize_async
data_actions
error_handling
event_synchronization
factorial
fibonacci
fibonacci_await
fibonacci_one
fibonacci_futures
fibonacci_futures_distributed
fractals
fractals_struct
safe_object
sierpinski
interest_calculator
interval_timer
latch_local
latch_remote
non_atomic_rma
partitioned_vector_spmd_foreach
pingpong
print_to_console
quicksort
shared_mutex
simple_future_continuation
simplest_hello_world
timed_futures
timed_wake
use_main_thread
wait_composition
zerocopy_rdma
)

set(allow_unknown_options_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(component_in_executable_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(component_ctors_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(component_inheritance_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(component_with_executor_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(customize_async_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(event_synchronization_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(error_handling_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(fractals_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(interval_timer_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(partitioned_vector_spmd_foreach_FLAGS
COMPONENT_DEPENDENCIES iostreams partitioned_vector)
set(pingpong_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(shared_mutex_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(simplest_hello_world_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(simple_future_continuation_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(timed_futures_FLAGS COMPONENT_DEPENDENCIES iostreams)

if(HPX_HAVE_LOCAL_SCHEDULER OR HPX_HAVE_ALL_SCHEDULERS)
set(example_programs ${example_programs}
fractals_executor
)
set(fractals_executor_FLAGS COMPONENT_DEPENDENCIES iostreams)
endif()

if(HPX_WITH_CXX11_LAMBDAS)
set(example_programs ${example_programs}
1d_wave_equation
hello_world
fibonacci_dataflow
pipeline1
vector_counting_dotproduct
vector_zip_dotproduct
)

set(1d_wave_equation_FLAGS COMPONENT_DEPENDENCIES iostreams)
if(HPX_HAVE_COMPRESSION_SNAPPY AND SNAPPY_FOUND)
set(hello_world_FLAGS COMPONENT_DEPENDENCIES iostreams DEPENDENCIES compress_snappy_lib)
else()
set(hello_world_FLAGS COMPONENT_DEPENDENCIES iostreams)
endif()
set(vector_counting_dotproduct_FLAGS COMPONENT_DEPENDENCIES iostreams)
set(vector_zip_dotproduct_FLAGS COMPONENT_DEPENDENCIES iostreams)
endif()

if(HPX_WITH_TUPLE_RVALUE_SWAP)
set(example_programs ${example_programs}
sort_by_key_demo
)
set(sort_by_key_demo_FLAGS COMPONENT_DEPENDENCIES iostreams)
endif()

foreach(example_program ${example_programs})
set(sources
${example_program}.cpp)

source_group("Source Files" FILES ${sources})

# add example executable
add_hpx_executable(${example_program}
SOURCES ${sources}
${${example_program}_FLAGS}
FOLDER "Examples/Quickstart")

# add a custom target for this example
add_hpx_pseudo_target(examples.quickstart.${example_program})

# make pseudo-targets depend on master pseudo-target
add_hpx_pseudo_dependencies(examples.quickstart
examples.quickstart.${example_program})

# add dependencies to pseudo-target
add_hpx_pseudo_dependencies(examples.quickstart.${example_program}
${example_program}_exe)
endforeach()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was added by accident

130 changes: 130 additions & 0 deletions examples/quickstart/sierpinski.cpp
@@ -0,0 +1,130 @@

// http://www.wikiwand.com/en/Sierpinski_triangle
// At each iteration, a white triangle in deleted from the middle of every black triangle,
// thereby splitting it into three equal triangles.

// Takes two program options, the number of iterations (n-value, with default 5)
// and the side length of the original triangle (side-length, with default 100)


#include <hpx/hpx_init.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/async.hpp>
#include <hpx/include/util.hpp>
#include <hpx/include/iostreams.hpp>
#include <hpx/include/serialization.hpp>

#include <iostream>

#include <boost/cstdint.hpp>
#include <boost/format.hpp>

// Class representing a Sierpinski triangle
class sierpinski{

public:
boost::uint64_t black_triangles, white_triangles;
double area;

sierpinski(){}
sierpinski(boost::uint64_t black, boost::uint64_t white, double area){
this->black_triangles = black;
this->white_triangles = white;
this->area = area;
}

inline sierpinski operator+(const sierpinski& other) const {
sierpinski res = sierpinski (black_triangles+other.black_triangles, white_triangles+other.white_triangles, area+other.area);
return res;
}


private:

//Serialization is necessary to transmit objects from one locality to another
friend class hpx::serialization::access;
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & black_triangles;
ar & white_triangles;
ar & area;
}
};


///////////////////////////////////////////////////////////////////////////////
// forward declaration of the get_sierpinski function
sierpinski get_sierpinski(boost::uint64_t n, double len);


// This is to generate the required boilerplate we need for the remote
// invocation to work.
HPX_PLAIN_ACTION(get_sierpinski, get_sierpinski_action);


///////////////////////////////////////////////////////////////////////////////
sierpinski get_sierpinski(boost::uint64_t n, double len)
{
if (n == 0)
return sierpinski(1, 0, sqrt(3)/4.0*len*len);

hpx::naming::id_type const locality_id = hpx::find_here();

// The problem for iteration n is broken down into three sub-problems,
// each of size n-1 (and side length gets halved). It is very inefficient as it does the same calculations
// three times but this is the method used for the sake of using parallelization.

get_sierpinski_action s;
hpx::future<sierpinski> n1 = hpx::async(s, locality_id, n - 1, len/2);
hpx::future<sierpinski> n2 = hpx::async(s, locality_id, n - 1, len/2);
hpx::future<sierpinski> n3 = hpx::async(s, locality_id, n - 1, len/2);

sierpinski ans = n1.get() + n2.get() + n3.get();
ans.white_triangles++;
return ans;
}


///////////////////////////////////////////////////////////////////////////////
int hpx_main(boost::program_options::variables_map& vm)
{
boost::uint64_t n = vm["n-value"].as<boost::uint64_t>();
boost::uint64_t len = vm["side-length"].as<boost::uint64_t>();

{
get_sierpinski_action s;
sierpinski ans = s(hpx::find_here(), n, (double)len);

hpx::cout << "After iteration: " << n << hpx::endl;
hpx::cout << "Black triangles: " << ans.black_triangles << hpx::endl;
hpx::cout << "White triangles: " << ans.white_triangles << hpx::endl;
hpx::cout << "Area: " << ans.area << hpx::endl;
}

return hpx::finalize();
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
// Configure application-specific options
boost::program_options::options_description
desc_commandline("Usage: " HPX_APPLICATION_STRING " [options]");

desc_commandline.add_options()
( "n-value",
boost::program_options::value<boost::uint64_t>()->default_value(5),
"n value for the Sierpinski function")
;
desc_commandline.add_options()
( "side-length",
boost::program_options::value<boost::uint64_t>()->default_value(100),
"side-length of the original triangle")
;

return hpx::init(desc_commandline, argc, argv);
}