jjzjj

c++ - thrust::tuple in reduction 的自定义最小运算符

coder 2024-02-25 原文

我正在尝试对 zip 迭代器进行最小缩减,但使用自定义运算符仅考虑元组中的第二个字段(第一个字段是键,而第二个字段是值)实际上与减少有关)

但是,我无法让它工作,目前正在计算 vector 中存在的结果

下面的代码重现了这个问题:

#include <thrust/device_vector.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/tuple.h>
#include <thrust/sequence.h>

typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;

struct tuple_snd_min{
  __host__ __device__ 
  bool operator()(const DereferencedIteratorTuple& lhs,
                  const DereferencedIteratorTuple& rhs){
    return (thrust::get<1>(lhs) < thrust::get<1>(rhs));
  }
};


void f(){
    thrust::device_vector<unsigned int> X(10);
    thrust::device_vector<unsigned int> Y(10);

    thrust::sequence(X.begin(), X.end());
    thrust::sequence(Y.begin(), Y.end());

    X[0] = 5;
    Y[0] = 5;
    X[1] = 50;

    // X: 5 50 2 3 4 5 6 7 8 9
    // Y: 5 1  2 3 4 5 6 7 8 9

    typedef thrust::device_vector<unsigned int>::iterator UIntIterator;
    typedef thrust::tuple<UIntIterator, UIntIterator> IteratorTuple;

    thrust::zip_iterator<IteratorTuple> first = 
        thrust::make_zip_iterator(thrust::make_tuple(X.begin(), Y.begin()));

    thrust::tuple<unsigned int, unsigned int> init = first[0];
    thrust::tuple<unsigned int, unsigned int> min = 
        thrust::reduce(first, first + 10, init, tuple_snd_min());

    printf("(%d,%d)\n", thrust::get<0>(min), thrust::get<1>(min));
    // should return (50,1)
    // returns (0,0)   
}

感谢 Jared Hoberock 的评论,我得以解决此问题。

typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;

struct tuple_snd_min{
  __host__ __device__ 
  const DereferencedIteratorTuple& operator()(const DereferencedIteratorTuple& lhs, const DereferencedIteratorTuple& rhs)
  {
    if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) return lhs;
    else return rhs;
  }
};

最佳答案

这似乎是由于对 reduce 调用中的仿函数必须实现哪个操作的误解造成的。根据 documentation ,仿函数必须是一个二元函数的模型,其输出必须可以转换为输入类型。这是你的仿函数失败的地方。而不是这个

struct tuple_snd_min{
  __host__ __device__ 
  bool operator()(const DereferencedIteratorTuple& lhs,
                  const DereferencedIteratorTuple& rhs){
    return (thrust::get<1>(lhs) < thrust::get<1>(rhs));
  }
};

你的仿函数需要像这样定义:

struct tuple_snd_min{
  __host__ __device__ 
  int operator()(const DereferencedIteratorTuple& lhs,
                  const DereferencedIteratorTuple& rhs){
    return (thrust::get<1>(lhs) < thrust::get<1>(rhs)) ?
             thrust::get<1>(lhs) : thrust::get<1>(rhs);
  }
};

即该函数应该返回一个值而不是充当谓词。

[此答案是根据评论汇总并作为社区 wiki 条目发布的,以便将此问题从未回答的队列中删除]

关于c++ - thrust::tuple in reduction 的自定义最小运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24188761/

有关c++ - thrust::tuple in reduction 的自定义最小运算符的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  7. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  8. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  9. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐