以下代码用于测试cudaMemcpyAsync的同步行为.
#include <iostream>
#include <sys/time.h>
#define N 100000000
using namespace std;
int diff_ms(struct timeval t1, struct timeval t2)
{
return (((t1.tv_sec - t2.tv_sec) * 1000000) +
(t1.tv_usec - t2.tv_usec))/1000;
}
double sumall(double *v, int n)
{
double s=0;
for (int i=0; i<n; i++) s+=v[i];
return s;
}
int main()
{
int i;
cudaStream_t strm;
cudaStreamCreate(&strm);
double *h0;
double *h1;
cudaMallocHost(&h0,N*sizeof(double));
cudaMallocHost(&h1,N*sizeof(double));
for (i=0; i<N; i++) h0[i]=99./N;
double *d;
cudaMalloc(&d,N*sizeof(double));
struct timeval t1, t2; gettimeofday(&t1,NULL);
cudaMemcpyAsync(d,h0,N*sizeof(double),cudaMemcpyHostToDevice,strm);
gettimeofday(&t2, NULL); printf("cuda H->D %d …Run Code Online (Sandbox Code Playgroud) 我知道对于具有31个线程的1D线程块,它将被填充到32个线程以进行warp执行.具有31*31线程的2D块怎么样?warp scheduler会为每个维度填充1个额外的线程(即总共31个将被填充),或者这个2D块线程将被连接,只有最后一个线程将被填充(31*31 = 961; 961%32 = 1) ?
基本上我想做以下事情:
#define TYPE float
int main()
{
if (TYPE==float)...;
}
Run Code Online (Sandbox Code Playgroud)
当然它不会工作,也不确定如何实现它.
基本上我想读一个包含大量双打的二进制文件.不确定如何实现以下目标:
N=10000000
fin=open("sth.bin","rb")
data = struct.unpack('dddddd......',fin.read(8*N)) #of course not working, but this is what I want
fin.close()
Run Code Online (Sandbox Code Playgroud) 说我有一节课:
class A
{
private:
const int * const v;
public:
A();
}
Run Code Online (Sandbox Code Playgroud)
我想v在初始化列表中分配,我想我可以定义以下构造函数:
A::A():v((int*)malloc(10*sizeof(int))){}
Run Code Online (Sandbox Code Playgroud)
但是,必须以非标准的方式分配v,如下所示:
cudaMalloc(&v,10*sizeof(int));
Run Code Online (Sandbox Code Playgroud)
注意cudaMalloc是用于分配GPU内存的CUDA API.
我有以下代码(从虚函数和static_cast中窃取):
#include <iostream>
class Base
{
public:
virtual void foo() { std::cout << "Base::foo() \n"; }
};
class Derived : public Base
{
public:
virtual void foo() { std::cout << "Derived::foo() \n"; }
};
Run Code Online (Sandbox Code Playgroud)
如果我有:
int main()
{
Base base;
Derived& _1 = static_cast<Derived&>(base);
_1.foo();
}
Run Code Online (Sandbox Code Playgroud)
打印输出将是: Base::foo()
但是,如果我有:
int main()
{
Base * base;
Derived* _1 = static_cast<Derived*>(base);
_1->foo();
}
Run Code Online (Sandbox Code Playgroud)
打印输出将是: Segmentation fault: 11
老实说,我对两者都不太了解.有人可以根据上面的例子解释static_cast和虚方法之间的复杂性吗?顺便说一句,如果我想打印出" Derived::foo()" ,我该怎么办?
我有两个简单的测试线:
cout<<(cout<<"ok"<<endl, 8)<<endl;
cout<<(int i(8), 8)<<endl;
Run Code Online (Sandbox Code Playgroud)
第一行工作,但第二行编译失败
error: expected primary-expression before 'int'
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我确实需要在逗号运算符中声明.更具体地说,我想声明一些变量,获取它们的值,并从我的类构造函数的初始化列表中将它们分配给我的常量类成员.以下显示了我的意图.如果使用逗号运算符无法实现,还有其他建议吗?
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void readFile(const string & fileName, int & a, int & b)
{
fstream fin(fileName.c_str());
if (!fin.good()) {cerr<<"file not found!"<<endl; exit(1);}
string line;
getline(fin, line);
stringstream ss(line);
try {ss>>a>>b;}
catch (...) {cerr<<"the first two entries in file "<<fileName<<" have to be numbers!"<<endl; exit(1);}
fin.close();
}
class A
{
private:
const int _a;
const int _b; …Run Code Online (Sandbox Code Playgroud) 我经常要面对包含几个const变量的类的场景,这些变量需要在初始化列表中初始化,但是还需要提前一些其他方法来计算值.
例如,对于以下类:
class Base
{
protected:
const int a, b;
public:
Base(string file);
};
Run Code Online (Sandbox Code Playgroud)
我希望构造函数通过解析文件来提取一些变量,然后将提取的值分配给const变量(a和b).我曾经经常破坏逗号运算符(使用逗号运算符来初始化基类),但发现对于更复杂的情况非常不方便.关于更优雅的解决方案的任何建议?
一些python内置方法(例如vars)被一些相同的局部变量名称"隐藏".如何调用"隐藏"内置方法?
我在调试模式下使用python -m pdb XXX.py,并且无法vars在__builtins__字典中看到隐藏的内置方法():
(Pdb) dir(__builtins__)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
我试图练习以下代码:
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
virtual void f(){cout<<"A"<<endl;}
virtual ~A(){cout<<"destruct A"<<endl;}
};
int main()
{
A o1,o2;
vector <A > O;
O.push_back(o1);
cout<<"1"<<endl;
O.push_back(o2);
cout<<"test"<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果证明是:
1
destruct A
test
destruct A
destruct A
destruct A
destruct A
Run Code Online (Sandbox Code Playgroud)
对第一个析构函数的来源感到困惑.
我有以下代码由于编译失败 invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
#include <iostream>
#include <stdlib.h>
using namespace std;
#define N 3
void f(int *p)
{
*p=8;
}
class A
{
private:
int a;
public:
void init() const;
void print() const {cout<<a<<endl;}
};
void A::init() const
{
f(&a);
}
int main()
{
A a;
a.init();
a.print();
}
Run Code Online (Sandbox Code Playgroud)
这并不奇怪,因为"init"函数的"const"限定符.
但是,以下代码编译并运行顺利,
#include <iostream>
#include <stdlib.h>
using namespace std;
#define N 3
void f(void **pp)
{
*pp = new int[N];
}
class A
{
private:
int …Run Code Online (Sandbox Code Playgroud) 我有以下C++代码:
#include <iostream>
#include <vector>
using namespace std;
class A
{
private:
int i;
public:
void f1(const A& o);
void f2()
{
cout<<i<<endl;
}
};
void A::f1(const A& o)
{
o.f2();
}
Run Code Online (Sandbox Code Playgroud)
它只是不编译.有人可以解释一下吗?谢谢!