我正在尝试为复杂的
但是我的 vector 的大小在编译时不是固定的,所以我不确定如何在 但是 vector 需要初始化器。 如何将运行时所需的 vector 大小传递给初始化子句?到目前为止,我所拥有的如下:declare reduction pragma 中定义 vector 的初始值设定项。也就是说,我不能只拥有initializer( omp_priv=TComplexVector(10,0) )
typedef std::vector<complex<float>> TCmplxVec;
void ComplexAdd(TCmplxVec & x,TCmplxVec & y){
for (int i=0;i<x.size();i++)
{
x.real()+= y.real();
//... same for imaginary part and other operations
}
}
#pragma omp declare reduction(AddCmplx: TCmplxVec: \
ComplexAdd(&omp_out, &omp_in)) initializer( \
omp_priv={TCmplxVec(**here I want a variable length**,0} )
void DoSomeOperation ()
{
//TCmplxVec vec is empty and anotherVec not
//so each thread runs the inner loop serially
#pragma omp parallel for reduction(AddCmplx: vec)
for ( n=0 ; n<10 ; ++n )
{
for (m=0; m<=someLength; ++m){
vec[m] += anotherVec[m+someOffset dependend on n and else];
}
}
}
最佳答案
您现在必须深入挖掘才能在线找到它,但是在 OpenMP Standard 的第 2.15 节中,在讨论用户声明的归约的地方,您会发现“特殊标识符 omp_orig 也可以出现在初始化子句中,它将引用要归约的原始变量的存储。”
所以你可以使用initializer (omp_priv=TCmplxVec(omp_orig.size(),0)),或者只是initializer (omp_priv(omp_orig))来初始化 vector 在减少。
所以下面的工作(请注意,您不需要编写自己的例程;您可以使用 std::transform 和 std::plus 来添加您的 vector ;您也可以使用 std::valarray 而不是 vector ,取决于您如何使用它们,其中已经定义了 operator+):
#include <complex>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <omp.h>
typedef std::vector< std::complex<float> > TCmplxVec;
#pragma omp declare reduction( + : TCmplxVec : \
std::transform(omp_in.begin( ), omp_in.end( ), \
omp_out.begin( ), omp_out.begin( ), \
std::plus< std::complex<float> >( )) ) \
initializer (omp_priv(omp_orig))
int main(int argc, char *argv[]) {
int size;
if (argc < 2)
size = 10;
else
size = atoi(argv[1]);
TCmplxVec result(size,0);
#pragma omp parallel reduction( + : result )
{
int tid=omp_get_thread_num();
for (int i=0; i<std::min(tid+1,size); i++)
result[i] += tid;
}
for (int i=0; i<size; i++)
std::cout << i << "\t" << result[i] << std::endl;
return 0;
}
运行这个给出
$ OMP_NUM_THREADS=1 ./reduction 8
0 (0,0)
1 (0,0)
2 (0,0)
3 (0,0)
4 (0,0)
5 (0,0)
6 (0,0)
7 (0,0)
$ OMP_NUM_THREADS=4 ./reduction 8
0 (6,0)
1 (6,0)
2 (5,0)
3 (3,0)
4 (0,0)
5 (0,0)
6 (0,0)
7 (0,0)
$ OMP_NUM_THREADS=8 ./reduction 8
0 (28,0)
1 (28,0)
2 (27,0)
3 (25,0)
4 (22,0)
5 (18,0)
6 (13,0)
7 (7,0)
关于c++ - 用户定义的不同大小 vector 的减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29633531/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>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
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用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"=>