/usr/include/thrust/detail/internal_functional.h is in libthrust-dev 1.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | /*
* Copyright 2008-2012 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file internal_functional.inl
* \brief Non-public functionals used to implement algorithm internals.
*/
#pragma once
#include <thrust/tuple.h>
#include <thrust/iterator/iterator_traits.h>
#include <memory> // for ::new
namespace thrust
{
namespace detail
{
// unary_negate does not need to know argument_type
template <typename Predicate>
struct unary_negate
{
typedef bool result_type;
Predicate pred;
__host__ __device__
explicit unary_negate(const Predicate& pred) : pred(pred) {}
template <typename T>
__host__ __device__
bool operator()(const T& x)
{
return !bool(pred(x));
}
};
// binary_negate does not need to know first_argument_type or second_argument_type
template <typename Predicate>
struct binary_negate
{
typedef bool result_type;
Predicate pred;
__host__ __device__
explicit binary_negate(const Predicate& pred) : pred(pred) {}
template <typename T1, typename T2>
__host__ __device__
bool operator()(const T1& x, const T2& y)
{
return !bool(pred(x,y));
}
};
template<typename Predicate>
__host__ __device__
thrust::detail::unary_negate<Predicate> not1(const Predicate &pred)
{
return thrust::detail::unary_negate<Predicate>(pred);
}
template<typename Predicate>
__host__ __device__
thrust::detail::binary_negate<Predicate> not2(const Predicate &pred)
{
return thrust::detail::binary_negate<Predicate>(pred);
}
// convert a predicate to a 0 or 1 integral value
template <typename Predicate, typename IntegralType>
struct predicate_to_integral
{
Predicate pred;
__host__ __device__
explicit predicate_to_integral(const Predicate& pred) : pred(pred) {}
template <typename T>
__host__ __device__
bool operator()(const T& x)
{
return pred(x) ? IntegralType(1) : IntegralType(0);
}
};
// note that detail::equal_to does not force conversion from T2 -> T1 as equal_to does
template <typename T1>
struct equal_to
{
typedef bool result_type;
template <typename T2>
__host__ __device__
bool operator()(const T1& lhs, const T2& rhs) const
{
return lhs == rhs;
}
};
// note that equal_to_value does not force conversion from T2 -> T1 as equal_to does
template <typename T2>
struct equal_to_value
{
T2 rhs;
equal_to_value(const T2& rhs) : rhs(rhs) {}
template <typename T1>
__host__ __device__
bool operator()(const T1& lhs) const
{
return lhs == rhs;
}
};
template <typename Predicate>
struct tuple_binary_predicate
{
typedef bool result_type;
__host__ __device__
tuple_binary_predicate(const Predicate& p) : pred(p) {}
template<typename Tuple>
__host__ __device__
bool operator()(const Tuple& t) const
{
return pred(thrust::get<0>(t), thrust::get<1>(t));
}
Predicate pred;
};
template <typename Predicate>
struct tuple_not_binary_predicate
{
typedef bool result_type;
__host__ __device__
tuple_not_binary_predicate(const Predicate& p) : pred(p) {}
template<typename Tuple>
__host__ __device__
bool operator()(const Tuple& t) const
{
return !pred(thrust::get<0>(t), thrust::get<1>(t));
}
Predicate pred;
};
template<typename Generator>
struct host_generate_functor
{
typedef void result_type;
__host__ __device__
host_generate_functor(Generator g)
: gen(g) {}
// operator() does not take an lvalue reference because some iterators
// produce temporary proxy references when dereferenced. for example,
// consider the temporary tuple of references produced by zip_iterator.
// such temporaries cannot bind to an lvalue reference.
//
// to WAR this, accept a const reference (which is bindable to a temporary),
// and const_cast in the implementation.
//
// XXX change to an rvalue reference upon c++0x (which either a named variable
// or temporary can bind to)
template<typename T>
__host__
void operator()(const T &x)
{
// we have to be naughty and const_cast this to get it to work
T &lvalue = const_cast<T&>(x);
// this assigns correctly whether x is a true reference or proxy
lvalue = gen();
}
Generator gen;
};
template<typename Generator>
struct device_generate_functor
{
typedef void result_type;
__host__ __device__
device_generate_functor(Generator g)
: gen(g) {}
// operator() does not take an lvalue reference because some iterators
// produce temporary proxy references when dereferenced. for example,
// consider the temporary tuple of references produced by zip_iterator.
// such temporaries cannot bind to an lvalue reference.
//
// to WAR this, accept a const reference (which is bindable to a temporary),
// and const_cast in the implementation.
//
// XXX change to an rvalue reference upon c++0x (which either a named variable
// or temporary can bind to)
template<typename T>
__host__ __device__
void operator()(const T &x)
{
// we have to be naughty and const_cast this to get it to work
T &lvalue = const_cast<T&>(x);
// this assigns correctly whether x is a true reference or proxy
lvalue = gen();
}
Generator gen;
};
template<typename System, typename Generator>
struct generate_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_generate_functor<Generator> >,
thrust::detail::identity_<device_generate_functor<Generator> >
>
{};
template<typename ResultType, typename BinaryFunction>
struct zipped_binary_op
{
typedef ResultType result_type;
__host__ __device__
zipped_binary_op(BinaryFunction binary_op)
: m_binary_op(binary_op) {}
template<typename Tuple>
__host__ __device__
inline result_type operator()(Tuple t)
{
return m_binary_op(thrust::get<0>(t), thrust::get<1>(t));
}
BinaryFunction m_binary_op;
};
template<typename UnaryFunction>
struct host_unary_transform_functor
{
typedef void result_type;
UnaryFunction f;
host_unary_transform_functor(UnaryFunction f_)
:f(f_) {}
template<typename Tuple>
__host__
inline result_type operator()(Tuple t)
{
thrust::get<1>(t) = f(thrust::get<0>(t));
}
};
template<typename UnaryFunction>
struct device_unary_transform_functor
{
typedef void result_type;
UnaryFunction f;
device_unary_transform_functor(UnaryFunction f_)
:f(f_) {}
// add __host__ to allow the omp backend compile with nvcc
template<typename Tuple>
__host__ __device__
inline result_type operator()(Tuple t)
{
thrust::get<1>(t) = f(thrust::get<0>(t));
}
};
template<typename System, typename UnaryFunction>
struct unary_transform_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_unary_transform_functor<UnaryFunction> >,
thrust::detail::identity_<device_unary_transform_functor<UnaryFunction> >
>
{};
template <typename BinaryFunction>
struct host_binary_transform_functor
{
BinaryFunction f;
host_binary_transform_functor(BinaryFunction f_)
:f(f_)
{}
template <typename Tuple>
__host__
void operator()(Tuple t)
{
thrust::get<2>(t) = f(thrust::get<0>(t), thrust::get<1>(t));
}
}; // end binary_transform_functor
template <typename BinaryFunction>
struct device_binary_transform_functor
{
BinaryFunction f;
device_binary_transform_functor(BinaryFunction f_)
:f(f_)
{}
// add __host__ to allow the omp backend compile with nvcc
template <typename Tuple>
__host__ __device__
void operator()(Tuple t)
{
thrust::get<2>(t) = f(thrust::get<0>(t), thrust::get<1>(t));
}
}; // end binary_transform_functor
template<typename System, typename BinaryFunction>
struct binary_transform_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_binary_transform_functor<BinaryFunction> >,
thrust::detail::identity_<device_binary_transform_functor<BinaryFunction> >
>
{};
template <typename UnaryFunction, typename Predicate>
struct host_unary_transform_if_functor
{
UnaryFunction unary_op;
Predicate pred;
host_unary_transform_if_functor(UnaryFunction unary_op_, Predicate pred_)
: unary_op(unary_op_), pred(pred_) {}
template<typename Tuple>
__host__
void operator()(Tuple t)
{
if(pred(thrust::get<0>(t)))
{
thrust::get<1>(t) = unary_op(thrust::get<0>(t));
}
}
}; // end host_unary_transform_if_functor
template <typename UnaryFunction, typename Predicate>
struct device_unary_transform_if_functor
{
UnaryFunction unary_op;
Predicate pred;
device_unary_transform_if_functor(UnaryFunction unary_op_, Predicate pred_)
: unary_op(unary_op_), pred(pred_) {}
template<typename Tuple>
__host__ __device__
void operator()(Tuple t)
{
if(pred(thrust::get<0>(t)))
{
thrust::get<1>(t) = unary_op(thrust::get<0>(t));
}
}
}; // end device_unary_transform_if_functor
template<typename System, typename UnaryFunction, typename Predicate>
struct unary_transform_if_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_unary_transform_if_functor<UnaryFunction,Predicate> >,
thrust::detail::identity_<device_unary_transform_if_functor<UnaryFunction,Predicate> >
>
{};
template <typename UnaryFunction, typename Predicate>
struct host_unary_transform_if_with_stencil_functor
{
UnaryFunction unary_op;
Predicate pred;
host_unary_transform_if_with_stencil_functor(UnaryFunction _unary_op, Predicate _pred)
: unary_op(_unary_op), pred(_pred) {}
template <typename Tuple>
__host__
void operator()(Tuple t)
{
if(pred(thrust::get<1>(t)))
thrust::get<2>(t) = unary_op(thrust::get<0>(t));
}
}; // end host_unary_transform_if_with_stencil_functor
template <typename UnaryFunction, typename Predicate>
struct device_unary_transform_if_with_stencil_functor
{
UnaryFunction unary_op;
Predicate pred;
device_unary_transform_if_with_stencil_functor(UnaryFunction _unary_op, Predicate _pred)
: unary_op(_unary_op), pred(_pred) {}
// add __host__ to allow the omp backend compile with nvcc
template <typename Tuple>
__host__ __device__
void operator()(Tuple t)
{
if(pred(thrust::get<1>(t)))
thrust::get<2>(t) = unary_op(thrust::get<0>(t));
}
}; // end device_unary_transform_if_with_stencil_functor
template<typename System, typename UnaryFunction, typename Predicate>
struct unary_transform_if_with_stencil_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_unary_transform_if_with_stencil_functor<UnaryFunction,Predicate> >,
thrust::detail::identity_<device_unary_transform_if_with_stencil_functor<UnaryFunction,Predicate> >
>
{};
template <typename BinaryFunction, typename Predicate>
struct host_binary_transform_if_functor
{
BinaryFunction binary_op;
Predicate pred;
host_binary_transform_if_functor(BinaryFunction _binary_op, Predicate _pred)
: binary_op(_binary_op), pred(_pred) {}
template <typename Tuple>
__host__
void operator()(Tuple t)
{
if(pred(thrust::get<2>(t)))
thrust::get<3>(t) = binary_op(thrust::get<0>(t), thrust::get<1>(t));
}
}; // end host_binary_transform_if_functor
template <typename BinaryFunction, typename Predicate>
struct device_binary_transform_if_functor
{
BinaryFunction binary_op;
Predicate pred;
device_binary_transform_if_functor(BinaryFunction _binary_op, Predicate _pred)
: binary_op(_binary_op), pred(_pred) {}
// add __host__ to allow the omp backend compile with nvcc
template <typename Tuple>
__host__ __device__
void operator()(Tuple t)
{
if(pred(thrust::get<2>(t)))
thrust::get<3>(t) = binary_op(thrust::get<0>(t), thrust::get<1>(t));
}
}; // end device_binary_transform_if_functor
template<typename System, typename BinaryFunction, typename Predicate>
struct binary_transform_if_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_binary_transform_if_functor<BinaryFunction,Predicate> >,
thrust::detail::identity_<device_binary_transform_if_functor<BinaryFunction,Predicate> >
>
{};
template<typename T>
struct host_destroy_functor
{
__host__
void operator()(T &x) const
{
x.~T();
} // end operator()()
}; // end host_destroy_functor
template<typename T>
struct device_destroy_functor
{
// add __host__ to allow the omp backend to compile with nvcc
__host__ __device__
void operator()(T &x) const
{
x.~T();
} // end operator()()
}; // end device_destroy_functor
template<typename System, typename T>
struct destroy_functor
: thrust::detail::eval_if<
thrust::detail::is_convertible<System, thrust::host_system_tag>::value,
thrust::detail::identity_<host_destroy_functor<T> >,
thrust::detail::identity_<device_destroy_functor<T> >
>
{};
template <typename T>
struct fill_functor
{
const T exemplar;
fill_functor(const T& _exemplar)
: exemplar(_exemplar) {}
__host__ __device__
T operator()(void) const
{
return exemplar;
}
};
template<typename T>
struct uninitialized_fill_functor
{
T exemplar;
uninitialized_fill_functor(T x):exemplar(x){}
__host__ __device__
void operator()(T &x)
{
::new(static_cast<void*>(&x)) T(exemplar);
} // end operator()()
}; // end uninitialized_fill_functor
// this predicate tests two two-element tuples
// we first use a Compare for the first element
// if the first elements are equivalent, we use
// < for the second elements
template<typename Compare>
struct compare_first_less_second
{
compare_first_less_second(Compare c)
: comp(c) {}
template<typename T1, typename T2>
__host__ __device__
bool operator()(T1 lhs, T2 rhs)
{
return comp(thrust::get<0>(lhs), thrust::get<0>(rhs)) || (!comp(thrust::get<0>(rhs), thrust::get<0>(lhs)) && thrust::get<1>(lhs) < thrust::get<1>(rhs));
}
Compare comp;
}; // end compare_first_less_second
} // end namespace detail
} // end namespace thrust
|