<commit_before>#include <unittest/unittest.h>
#include <thrust/extrema.h>
#include <thrust/execution_policy.h>


template<typename Iterator, typename Iterator2>
__global__
void max_element_kernel(Iterator first, Iterator last, Iterator2 result)
{
  *result = thrust::max_element(thrust::seq, first, last);
}


template<typename Iterator, typename BinaryPredicate, typename Iterator2>
__global__
void max_element_kernel(Iterator first, Iterator last, BinaryPredicate pred, Iterator2 result)
{
  *result = thrust::max_element(thrust::seq, first, last, pred);
}


template<typename T>
void TestMaxElementDeviceSeq(const size_t n)
{
  thrust::host_vector<T> h_data = unittest::random_samples<T>(n);
  thrust::device_vector<T> d_data = h_data;

  typedef typename thrust::device_vector<T>::iterator iter_type;

  thrust::device_vector<iter_type> d_result(1);
  
  typename thrust::host_vector<T>::iterator   h_max = thrust::max_element(h_data.begin(), h_data.end());

  max_element_kernel<<<1,1>>>(d_data.begin(), d_data.end(), d_result.begin());
  ASSERT_EQUAL(h_max - h_data.begin(), (iter_type)d_result[0] - d_data.begin());

  
  typename thrust::host_vector<T>::iterator   h_min = thrust::max_element(h_data.begin(), h_data.end(), thrust::greater<T>());

  max_element_kernel<<<1,1>>>(d_data.begin(), d_data.end(), thrust::greater<T>(), d_result.begin());
  ASSERT_EQUAL(h_min - h_data.begin(), (iter_type)d_result[0] - d_data.begin());
}
DECLARE_VARIABLE_UNITTEST(TestMaxElementDeviceSeq);

<commit_msg>Test max_element with thrust::device in __device__ code.<commit_after>#include <unittest/unittest.h>
#include <thrust/extrema.h>
#include <thrust/execution_policy.h>


template<typename ExecutionPolicy, typename Iterator, typename Iterator2>
__global__
void max_element_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Iterator2 result)
{
  *result = thrust::max_element(exec, first, last);
}


template<typename ExecutionPolicy, typename Iterator, typename BinaryPredicate, typename Iterator2>
__global__
void max_element_kernel(ExecutionPolicy exec, Iterator first, Iterator last, BinaryPredicate pred, Iterator2 result)
{
  *result = thrust::max_element(exec, first, last, pred);
}


template<typename T, typename ExecutionPolicy>
void TestMaxElementDevice(ExecutionPolicy exec, const size_t n)
{
  thrust::host_vector<T> h_data = unittest::random_samples<T>(n);
  thrust::device_vector<T> d_data = h_data;

  typedef typename thrust::device_vector<T>::iterator iter_type;

  thrust::device_vector<iter_type> d_result(1);
  
  typename thrust::host_vector<T>::iterator   h_max = thrust::max_element(h_data.begin(), h_data.end());

  max_element_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_result.begin());
  ASSERT_EQUAL(h_max - h_data.begin(), (iter_type)d_result[0] - d_data.begin());

  
  typename thrust::host_vector<T>::iterator   h_min = thrust::max_element(h_data.begin(), h_data.end(), thrust::greater<T>());

  max_element_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), thrust::greater<T>(), d_result.begin());
  ASSERT_EQUAL(h_min - h_data.begin(), (iter_type)d_result[0] - d_data.begin());
}


template<typename T>
void TestMaxElementDeviceSeq(const size_t n)
{
  TestMaxElementDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestMaxElementDeviceSeq);


template<typename T>
void TestMaxElementDeviceDevice(const size_t n)
{
  TestMaxElementDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestMaxElementDeviceDevice);



{'fraction_non_alphanumeric': 0.14187248707639288, 'fraction_numerical': 0.006318207926479035, 'mean_word_length': 7.7075, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501037', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1181, 'n_words': 213}