111 lines
2.6 KiB
C++
111 lines
2.6 KiB
C++
// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
|
|
//
|
|
// For full license terms please see the LICENSE file distributed with this
|
|
// source code
|
|
|
|
#include "STDStream.h"
|
|
|
|
#include <algorithm>
|
|
#include <execution>
|
|
#include <numeric>
|
|
|
|
// There are three execution policies:
|
|
// auto exe_policy = std::execution::seq;
|
|
// auto exe_policy = std::execution::par;
|
|
auto exe_policy = std::execution::par_unseq;
|
|
|
|
|
|
template <class T>
|
|
STDStream<T>::STDStream(const int ARRAY_SIZE, int device)
|
|
noexcept : array_size{ARRAY_SIZE}, range(0, array_size), a(array_size), b(array_size), c(array_size)
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::init_arrays(T initA, T initB, T initC)
|
|
{
|
|
std::fill(exe_policy, a.begin(), a.end(), initA);
|
|
std::fill(exe_policy, b.begin(), b.end(), initB);
|
|
std::fill(exe_policy, c.begin(), c.end(), initC);
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::read_arrays(std::vector<T>& h_a, std::vector<T>& h_b, std::vector<T>& h_c)
|
|
{
|
|
h_a = a;
|
|
h_b = b;
|
|
h_c = c;
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::copy()
|
|
{
|
|
// c[i] = a[i]
|
|
std::copy(exe_policy, a.begin(), a.end(), c.begin());
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::mul()
|
|
{
|
|
// b[i] = scalar * c[i];
|
|
std::transform(exe_policy, range.begin(), range.end(), b.begin(), [&, scalar = startScalar](int i) {
|
|
return scalar * c[i];
|
|
});
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::add()
|
|
{
|
|
// c[i] = a[i] + b[i];
|
|
std::transform(exe_policy, range.begin(), range.end(), c.begin(), [&](int i) {
|
|
return a[i] + b[i];
|
|
});
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::triad()
|
|
{
|
|
// a[i] = b[i] + scalar * c[i];
|
|
std::transform(exe_policy, range.begin(), range.end(), a.begin(), [&, scalar = startScalar](int i) {
|
|
return b[i] + scalar * c[i];
|
|
});
|
|
}
|
|
|
|
template <class T>
|
|
void STDStream<T>::nstream()
|
|
{
|
|
// a[i] += b[i] + scalar * c[i];
|
|
// Need to do in two stages with C++11 STL.
|
|
// 1: a[i] += b[i]
|
|
// 2: a[i] += scalar * c[i];
|
|
std::transform(exe_policy, range.begin(), range.end(), a.begin(), [&, scalar = startScalar](int i) {
|
|
return a[i] + b[i] + scalar * c[i];
|
|
});
|
|
}
|
|
|
|
|
|
template <class T>
|
|
T STDStream<T>::dot()
|
|
{
|
|
// sum = 0; sum += a[i]*b[i]; return sum;
|
|
return std::transform_reduce(exe_policy, a.begin(), a.end(), b.begin(), 0.0);
|
|
}
|
|
|
|
void listDevices(void)
|
|
{
|
|
std::cout << "Listing devices is not supported by the Parallel STL" << std::endl;
|
|
}
|
|
|
|
std::string getDeviceName(const int)
|
|
{
|
|
return std::string("Device name unavailable");
|
|
}
|
|
|
|
std::string getDeviceDriver(const int)
|
|
{
|
|
return std::string("Device driver unavailable");
|
|
}
|
|
template class STDStream<float>;
|
|
template class STDStream<double>;
|
|
|