int main()
{
std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
std::for_each(v.begin(), v.end(), std::cout << boost::lambda::_1 << "\n");
}
Run Code Online (Sandbox Code Playgroud)
可以在不使用Boost的情况下将此代码转换为C++吗?我知道C++ 0x lambda表达式语法,但没有尝试在这样的上下文中使用占位符.
MSVC 9和g ++ - 4.5不同意对使用typename中nested::baz的下方.哪个是对的?
template<typename T>
struct foo
{
typedef T type;
};
template<typename T>
typename foo<T>::type
bar(T x)
{
struct nested
{
inline static typename foo<T>::type baz(T x)
{
typename foo<T>::type result;
return result;
}
};
return nested::baz(x);
}
int main()
{
int x;
bar(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
MSVC的输出:
$ cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(15) : error C2899: …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我正在构建一个函数,marshal_and_apply它f使用一些参数调用其他函数(或函子).marshal_and_apply的工作是根据参数的类型对参数应用一些特殊的编组f.
如果一个f的参数是一种特殊类型的,marshal_me<T>,然后marshal_and_apply将它传递给元帅之前通过一些特殊分配的存储参数f.为了执行分配,必须知道所有参数的存储要求,marshal_and_apply然后才能对其进行编组.
一些例子:
template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args... args);
void func1(int x, int y);
void func2(marshal_me<int> x, int y);
void func3(marshal_me<int> x, marshal_me<int> y, marshal_me<int> z);
// this call would be equivalent to:
// func1(7,13)
marshal_and_apply(func1, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int));
// auto x = marshal_me<int>(7, storage);
// func2(x, 13); …Run Code Online (Sandbox Code Playgroud) 我的构建结构如下:
SConstruct
subdir/SConscript
subdir/module/__init__.py
Run Code Online (Sandbox Code Playgroud)
SConstructsubdir/SConscript作为子公司调用:
# SConstruct
SConscript('subdir/SConscript')
Run Code Online (Sandbox Code Playgroud)
subdir/SConscript进口module:
# subdir/SConscript
from module import foo
do SConsy stuff with foo()...
Run Code Online (Sandbox Code Playgroud)
这工作正常,直到我使用variant_dirwith subdir/SConscript:
# SConstruct
SConscript('subdir/SConscript', variant_dir='subdir/build', duplicate=0)
Run Code Online (Sandbox Code Playgroud)
问题是import失败,因为module不再位于路径中,该路径已被 更改variant_dir。
SCons 或 Python 是否有解决此问题的标准方法?我知道特殊site_scons目录,但看来该目录必须存在于 root 的顶层SConstruct,并且我想将subdir特定文件保留在subdir.
ISO C++ 11规范的第5.1.2节第10节规定:
使用通常的非限定名称查找规则(3.4.1)查找捕获列表中的标识符; 每个这样的查找应该找到一个变量,其自动存储持续时间在本地lambda表达式的到达范围内声明.如果实体(即变量或此实体)出现在lambda表达式的捕获列表中,则称其被明确捕获.
这似乎意味着lambda无法捕获文件范围变量.例如,该程序应该是非法的:
#include <iostream>
int x = 13;
int main()
{
auto l = [](){ return x; };
std::cout << l() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,g++4.7.1会产生我期望的结果:
$ g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++11 lambda.cpp
$ ./a.out
13
Run Code Online (Sandbox Code Playgroud)
但clang …
我正在尝试.bc在运行时加载文件中定义的LLVM模块,但遇到了麻烦.
感兴趣的bitcode来自hello.cpp:
// hello.cpp
// build with:
// clang-3.4 -c -emit-llvm hello.cpp -o hello.bc
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当下面的程序试图在运行时加载它时,它崩溃在里面llvm::BitstreamCursor::Read():
// main.cpp
// build with:
// g++ main.cpp `llvm-config-3.4 --cppflags --ldflags --libs` -ldl -lpthread -lcurses
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <fstream>
#include <iostream>
llvm::Module *load_module(std::ifstream &stream)
{
if(!stream)
{
std::cerr << "error after open stream" << std::endl;
return 0; …Run Code Online (Sandbox Code Playgroud) 以下程序尝试std::tuple_element为用户定义的类型提供特化foo.不幸的是,clang-3.5用libc ++拒绝它,但是使用其他编译器或使用其他标准库来clang-3.5接受程序.这是对的吗?如果不是,为什么不呢?
#include <utility>
struct foo {};
namespace std
{
template<size_t, class> struct tuple_element;
template<size_t i>
struct tuple_element<i, foo> {};
}
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
$ clang-3.5 -std=c++11 -stdlib=libc++ -lc++ test.cpp
test.cpp:11:8: error: explicit specialization of non-template struct 'tuple_element'
struct tuple_element<i, foo> {};
^ ~~~~~~~~
1 error generated.
$ clang-3.5 -std=c++11 -lstdc++ test.cpp
(no error)
$ g++-4.9 -std=c++11 test.cpp
(no error)
Run Code Online (Sandbox Code Playgroud) 我正在使用推力分区功能将数组划分为偶数和奇数.但是,当我尝试显示设备矢量时,它会显示随机值.请告诉我错误在哪里.我想我做的一切都是正确的.
#include<stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/partition.h>
struct is_even
{
//const int toCom;
//is_even(int val):toCom(val){}
__device__
bool operator()(const int &x)
{
return x%2;
}
};
void main(){
thrust::host_vector<int> H(6);
for(int i =0 ; i<H.size();i++){
H[i] = i+1;
}
thrust::device_vector<int> D = H;
thrust::partition(D.begin(),D.end(),is_even());
for(int i =0 ;i< D.size();i++){
printf("%d,",D[i]);
}
getchar();
}
Run Code Online (Sandbox Code Playgroud) 我有一个5000x500矩阵,我想用cuda分别对每行进行排序.我可以使用arrayfire,但这只是关于thrust :: sort的for循环,这应该不高效.
https://github.com/arrayfire/arrayfire/blob/devel/src/backend/cuda/kernel/sort.hpp
for(dim_type w = 0; w < val.dims[3]; w++) {
dim_type valW = w * val.strides[3];
for(dim_type z = 0; z < val.dims[2]; z++) {
dim_type valWZ = valW + z * val.strides[2];
for(dim_type y = 0; y < val.dims[1]; y++) {
dim_type valOffset = valWZ + y * val.strides[1];
if(isAscending) {
thrust::sort(val_ptr + valOffset, val_ptr + valOffset + val.dims[0]);
} else {
thrust::sort(val_ptr + valOffset, val_ptr + valOffset + val.dims[0],
thrust::greater<T>());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法融合推力操作,以便排序并行?实际上,我正在寻找的是融合循环迭代的通用方法.
我想创建一个类型特征,它可以检测其他一些感兴趣类型中是否存在嵌套类模板.
例如,假设我想创建类型trait has_foo,它检测foo某个类型中命名的一个参数的嵌套模板的存在T:
#include <cassert>
template<class T>
struct has_foo
{
// XXX what goes here?
};
struct with_foo
{
template<class T> struct foo {};
};
struct without_foo {};
int main()
{
assert(has_foo<with_foo>::value);
assert(!has_foo<without_foo>::value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
实施的最佳方式是has_foo什么?
我需要执行并行缩减以在CUDA设备上找到数组的最小值或最大值.我找到了一个很好的图书馆,称为Thrust.您似乎只能在主机内存中对阵列执行并行缩减.我的数据在设备内存中.是否可以减少设备内存中的数据?我无法想象如何做到这一点.以下是Thrust的文档:http://code.google.com/p/thrust/wiki/QuickStartGuide#Reductions.谢谢大家.
我们有以下串行C代码在运行
两个向量a []和b []:
double a[20000],b[20000],r=0.9;
for(int i=1;i<=10000;++i)
{
a[i]=r*a[i]+(1-r)*b[i]];
errors=max(errors,fabs(a[i]-b[i]);
b[i]=a[i];
}
Run Code Online (Sandbox Code Playgroud)
请告诉我们如何将此代码移植到CUDA和Cublas?