Как объединить сравнения тяги с использованием заполнителей? - PullRequest
0 голосов
/ 10 декабря 2018

У меня есть device_vector значений A с плавающей запятой размера N. У меня также есть значение V с плавающей запятой для сравнения.В зависимости от входного значения мне нужно извлечь индексы A, для которых значения> OR

Я использую следующий код, но он кажется громоздким.Есть ли более лаконичный способ сделать это?

void detect_indices_lesser_greater_equal_to_value(thrust::device_vector<float> S, float value, 
                        int criterion, thrust::device_vector<int>& indices)
{

int N=S.size();

int size=N;


if(criterion==0) // criterion =0 => equal
{
thrust::device_vector<int>::iterator end = thrust::copy_if(thrust::device,thrust::make_counting_iterator(0),
                                                             thrust::make_counting_iterator(N),
                                                             S.begin(),
                                                             indices.begin(), 
                                                             thrust::placeholders::_1 == value);
size = end-indices.begin();
}


if(criterion==1) // criterion =1 => less
{
thrust::device_vector<int>::iterator end = thrust::copy_if(thrust::device,thrust::make_counting_iterator(0),
                                                             thrust::make_counting_iterator(N),
                                                             S.begin(),
                                                             indices.begin(), 
                                                             thrust::placeholders::_1 < value);
size = end-indices.begin();
}

if(criterion==2) // criterion =2 => greater
{
thrust::device_vector<int>::iterator end = thrust::copy_if(thrust::device,thrust::make_counting_iterator(0),
                                                             thrust::make_counting_iterator(N),
                                                             S.begin(),
                                                             indices.begin(), 
                                                             thrust::placeholders::_1 > value);
size = end-indices.begin();
}

indices.resize(size);

}

1 Ответ

0 голосов
/ 11 декабря 2018

Это можно сделать с помощью двух thrust::partition операций.Разбиение довольно простое: все, что приводит к истинному предикату, перемещается в левую часть входного вектора.Все остальное смещено вправо.Вот простой пример:

$ cat t22.cu
#include <thrust/partition.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>

typedef float mt;

using namespace thrust::placeholders;
int main(){

  const mt pval = 4;
  mt data[] = {1,3,7,4,5,2,4,3,9};
  const int ds = sizeof(data)/sizeof(data[0]);
  thrust::device_vector<mt> d(data, data+ds);
  auto end1 = thrust::partition(d.begin(), d.end(), _1<pval);
  auto end2 = thrust::partition(end1, d.end(), _1==pval);
  std::cout << "less than pval:" << std::endl;
  thrust::copy(d.begin(), end1, std::ostream_iterator<mt>(std::cout,","));
  std::cout << std::endl << "equal to pval:" << std::endl;
  thrust::copy(end1, end2, std::ostream_iterator<mt>(std::cout,","));
  std::cout << std::endl << "greater than pval:" << std::endl;
  thrust::copy(end2, d.end(), std::ostream_iterator<mt>(std::cout,","));
  std::cout << std::endl;
}
$ nvcc -o t22 t22.cu
$ ./t22
less than pval:
1,3,2,3,
equal to pval:
4,4,
greater than pval:
7,5,9,
$

Если вам требуется, чтобы упорядочение в 3 результирующих субвекторах было таким же, как в исходном упорядочении ввода, вы можете использовать вариант thrust::stable_partition.

(Обратите внимание, что в вашем вопросе вы ссылаетесь на float количества, но ваш пример кода использует итераторы <int>. Однако приведенный выше код может работать с любым из них путем изменения typedef).

...