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

Embb10 heterogeneous algorithms #48

Merged
merged 4 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions algorithms_cpp/include/embb/algorithms/count.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef EMBB_ALGORITHMS_COUNT_H_
#define EMBB_ALGORITHMS_COUNT_H_

#include <embb/mtapi/job.h>
#include <embb/mtapi/execution_policy.h>
#include <iterator>

Expand Down Expand Up @@ -161,6 +162,18 @@ typename std::iterator_traits<RAI>::difference_type Count(
return Count(first, last, value, policy, 0);
}

/**
* Overload of above described Doxygen dummy.
*/
template<typename RAI>
typename std::iterator_traits<RAI>::difference_type CountIf(
RAI first,
RAI last,
embb::mtapi::Job comparison,
const embb::mtapi::ExecutionPolicy& policy,
size_t block_size
);

/**
* Overload of above described Doxygen dummy.
*/
Expand Down
13 changes: 13 additions & 0 deletions algorithms_cpp/include/embb/algorithms/for_each.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef EMBB_ALGORITHMS_FOR_EACH_H_
#define EMBB_ALGORITHMS_FOR_EACH_H_

#include <embb/mtapi/job.h>
#include <embb/mtapi/execution_policy.h>

namespace embb {
Expand Down Expand Up @@ -80,6 +81,18 @@ void ForEach(

#else // DOXYGEN

/**
* Overload of above described Doxygen dummy.
*/
template<typename RAI>
void ForEach(
RAI first,
RAI last,
embb::mtapi::Job unary,
const embb::mtapi::ExecutionPolicy& policy,
size_t block_size
);

/**
* Overload of above described Doxygen dummy.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2014-2016, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef EMBB_ALGORITHMS_INTERNAL_COMPARISON_JOB_FUNCTOR_H_
#define EMBB_ALGORITHMS_INTERNAL_COMPARISON_JOB_FUNCTOR_H_

#include <embb/mtapi/mtapi.h>

namespace embb {
namespace algorithms {
namespace internal {

template<typename ElementType>
class ComparisonJobFunctor {
public:
ComparisonJobFunctor(embb::mtapi::Job comparison,
embb::mtapi::ExecutionPolicy const & policy)
: comparison_(comparison) {
attr_.SetPolicy(policy);
}

bool operator () (ElementType const & v1, ElementType const & v2) const {
struct {
ElementType in1;
ElementType in2;
} inputs;
inputs.in1 = v1;
inputs.in2 = v2;
struct {
bool out;
} outputs;
embb::mtapi::Node & node = embb::mtapi::Node::GetInstance();
embb::mtapi::Task task = node.Start(
MTAPI_TASK_ID_NONE, comparison_.GetInternal(),
&inputs, sizeof(inputs),
&outputs, sizeof(outputs),
&attr_.GetInternal());
task.Wait(MTAPI_INFINITE);
return outputs.out;
}

private:
embb::mtapi::Job comparison_;
embb::mtapi::TaskAttributes attr_;
};

} // namespace internal
} // namespace algorithms
} // namespace embb

#endif // EMBB_ALGORITHMS_INTERNAL_COMPARISON_JOB_FUNCTOR_H_
14 changes: 14 additions & 0 deletions algorithms_cpp/include/embb/algorithms/internal/count-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <functional>
#include <embb/algorithms/reduce.h>
#include <embb/algorithms/internal/predicate_job_functor.h>

namespace embb {
namespace algorithms {
Expand Down Expand Up @@ -90,6 +91,19 @@ typename std::iterator_traits<RAI>::difference_type
block_size);
}

template<typename RAI>
typename std::iterator_traits<RAI>::difference_type
CountIf(RAI first, RAI last, embb::mtapi::Job comparison,
const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
typedef typename std::iterator_traits<RAI>::difference_type Difference;
typedef internal::PredicateJobFunctor<
typename std::iterator_traits<RAI>::value_type> Predicate;
return Reduce(first, last, Difference(0), std::plus<Difference>(),
internal::FunctionComparisonFunction<Predicate>(
Predicate(comparison, policy)),
policy, block_size);
}

template<typename RAI, typename ComparisonFunction>
typename std::iterator_traits<RAI>::difference_type
CountIf(RAI first, RAI last, ComparisonFunction comparison,
Expand Down
12 changes: 12 additions & 0 deletions algorithms_cpp/include/embb/algorithms/internal/for_each-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <embb/mtapi/mtapi.h>
#include <embb/algorithms/internal/partition.h>
#include <embb/algorithms/zip_iterator.h>
#include <embb/algorithms/internal/foreach_job_functor.h>

namespace embb {
namespace algorithms {
Expand Down Expand Up @@ -147,6 +148,17 @@ void ForEachIteratorCheck(RAI first, RAI last, Function unary,

} // namespace internal

template<typename RAI>
void ForEach(RAI first, const RAI last, embb::mtapi::Job unary,
const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
typename std::iterator_traits<RAI>::iterator_category category;
internal::ForEachIteratorCheck(first, last,
internal::ForeachJobFunctor<
typename std::iterator_traits<RAI>::value_type>(
unary, policy),
policy, block_size, category);
}

template<typename RAI, typename Function>
void ForEach(RAI first, const RAI last, Function unary,
const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2014-2016, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef EMBB_ALGORITHMS_INTERNAL_FOREACH_JOB_FUNCTOR_H_
#define EMBB_ALGORITHMS_INTERNAL_FOREACH_JOB_FUNCTOR_H_

#include <embb/mtapi/mtapi.h>

namespace embb {
namespace algorithms {
namespace internal {

template<typename ElementType>
class ForeachJobFunctor {
public:
ForeachJobFunctor(embb::mtapi::Job unary,
embb::mtapi::ExecutionPolicy const & policy)
: unary_(unary) {
attr_.SetPolicy(policy);
}

void operator () (ElementType & val) const {
struct {
ElementType in;
} inputs;
inputs.in = val;
struct {
ElementType out;
} outputs;
embb::mtapi::Node & node = embb::mtapi::Node::GetInstance();
embb::mtapi::Task task = node.Start(
MTAPI_TASK_ID_NONE, unary_.GetInternal(),
&inputs, sizeof(inputs),
&outputs, sizeof(outputs),
&attr_.GetInternal());
task.Wait(MTAPI_INFINITE);
val = outputs.out;
}

private:
embb::mtapi::Job unary_;
embb::mtapi::TaskAttributes attr_;
};

} // namespace internal
} // namespace algorithms
} // namespace embb

#endif // EMBB_ALGORITHMS_INTERNAL_FOREACH_JOB_FUNCTOR_H_
13 changes: 13 additions & 0 deletions algorithms_cpp/include/embb/algorithms/internal/merge_sort-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <embb/base/exceptions.h>
#include <embb/mtapi/mtapi.h>
#include <embb/algorithms/internal/partition.h>
#include <embb/algorithms/internal/comparison_job_functor.h>

namespace embb {
namespace algorithms {
Expand Down Expand Up @@ -264,6 +265,18 @@ void MergeSortIteratorCheck(

} // namespace internal

template<typename RAI, typename RAITemp>
void MergeSort(RAI first, RAI last, RAITemp temporary_first,
embb::mtapi::Job comparison, const embb::mtapi::ExecutionPolicy& policy,
size_t block_size) {
typedef typename std::iterator_traits<RAI>::iterator_category category;
internal::MergeSortIteratorCheck(first, last, temporary_first,
internal::ComparisonJobFunctor<
typename std::iterator_traits<RAI>::value_type>(
comparison, policy),
policy, block_size, category());
}

template<typename RAI, typename RAITemp, typename ComparisonFunction>
void MergeSort(RAI first, RAI last, RAITemp temporary_first,
ComparisonFunction comparison, const embb::mtapi::ExecutionPolicy& policy,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2014-2016, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef EMBB_ALGORITHMS_INTERNAL_PREDICATE_JOB_FUNCTOR_H_
#define EMBB_ALGORITHMS_INTERNAL_PREDICATE_JOB_FUNCTOR_H_

#include <embb/mtapi/mtapi.h>

namespace embb {
namespace algorithms {
namespace internal {

template<typename ElementType>
class PredicateJobFunctor {
public:
PredicateJobFunctor(embb::mtapi::Job comparison,
embb::mtapi::ExecutionPolicy const & policy)
: comparison_(comparison) {
attr_.SetPolicy(policy);
}

bool operator () (ElementType const & val) const {
struct {
ElementType in;
} inputs;
inputs.in = val;
struct {
bool out;
} outputs;
embb::mtapi::Node & node = embb::mtapi::Node::GetInstance();
embb::mtapi::Task task = node.Start(
MTAPI_TASK_ID_NONE, comparison_.GetInternal(),
&inputs, sizeof(inputs),
&outputs, sizeof(outputs),
&attr_.GetInternal());
task.Wait(MTAPI_INFINITE);
return outputs.out;
}

private:
embb::mtapi::Job comparison_;
embb::mtapi::TaskAttributes attr_;
};

} // namespace internal
} // namespace algorithms
} // namespace embb

#endif // EMBB_ALGORITHMS_INTERNAL_PREDICATE_JOB_FUNCTOR_H_
11 changes: 11 additions & 0 deletions algorithms_cpp/include/embb/algorithms/internal/quick_sort-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <embb/base/exceptions.h>
#include <embb/mtapi/mtapi.h>
#include <embb/algorithms/internal/partition.h>
#include <embb/algorithms/internal/comparison_job_functor.h>

namespace embb {
namespace algorithms {
Expand Down Expand Up @@ -227,6 +228,16 @@ void QuickSortIteratorCheck(RAI first, RAI last,

} // namespace internal

template <typename RAI>
void QuickSort(RAI first, RAI last, embb::mtapi::Job comparison,
const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
typedef typename std::iterator_traits<RAI>::iterator_category category;
internal::QuickSortIteratorCheck(first, last,
internal::ComparisonJobFunctor<
typename std::iterator_traits<RAI>::value_type>(comparison, policy),
policy, block_size, category());
}

template <typename RAI, typename ComparisonFunction>
void QuickSort(RAI first, RAI last, ComparisonFunction comparison,
const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
Expand Down
Loading