CUDA学习(八十)

简介:

代码示例:
数据聚合类:

class PixelRGBA {
public:
    __device__ PixelRGBA() : r_(0), g_(0), b_(0), a_(0) { }
    __device__ PixelRGBA(unsigned char r, unsigned char g,
        unsigned char b, unsigned char a = 255) :
        r_(r), g_(g), b_(b), a_(a) { }
private:
    unsigned char r_, g_, b_, a_;
    friend PixelRGBA operator+(const PixelRGBA const PixelRGBA&);
};
__device__
PixelRGBA operator+(const PixelRGBA& p1, const PixelRGBA& p2)
{
    return PixelRGBA(p1.r_ + p2.r_, p1.g_ + p2.g_,
        p1.b_ + p2.b_, p1.a_ + p2.a_);
}
__device__ void func(void)
{
    PixelRGBA p1, p2;
    // ... // Initialization of p1 and p2 here
    PixelRGBA p3 = p1 + p2;
}

派生类:

__device__ void* operator new(size_t bytes, MemoryPool& p);
__device__ void operator delete(void*, MemoryPool& p);
class Shape {
public:
__device__ Shape(void) { }
__device__ void putThis(PrintBuffer *p) const;
__device__ virtual void Draw(PrintBuffer *p) const {
p->put("Shapeless");
}
__device__ virtual ~Shape() {}
};
class Point : public Shape {
public:
__device__ Point() : x(0), y(0) {}
__device__ Point(int ix, int iy) : x(ix), y(iy) { }
__device__ void PutCoord(PrintBuffer *p) const;
__device__ void Draw(PrintBuffer *p) const;
__device__ ~Point() {}
private:
int x, y;
};
__device__ Shape* GetPointObj(MemoryPool& pool)
{
Shape* shape = new(pool) Point(rand(-20,10), rand(-100,-20));
return shape;
}

类模板:

template <class T>
class myValues {
    T values[MAX_VALUES];
public:
    __device__ myValues(T clear) { ... }
    __device__ void setValue(int Idx, T value) { ... }
    __device__ void putToMemory(T* valueLocation) { ... }
};
template <class T>
void __global__ useValues(T* memoryBuffer) {
    myValues<T> myLocation(0);
    ...
}
__device__ void* buffer;
int main()
{
    ...
        useValues<int> << <blocks, threads >> >(buffer);
    ...
}

功能模板:

template <typename T>
__device__ bool func(T x)
{
    ...
        return (...);
}
template <>
__device__ bool func<int>(T x) // Specialization
{
    return true;
}
// Explicit argument specification
bool result = func<double>(0.5);
// Implicit argument deduction
int x = 1;
bool result = func(x);

Functor Class:

class Add {
public:
    __device__ float operator() (float a, float b) const
    {
        return a + b;
    }
};
class Sub {
public:
    __device__ float operator() (float a, float b) const
    {
        return a - b;
    }
};
// Device code
template<class O> __global__
void VectorOperation(const float * A, const float * B, float * C,
    unsigned int N, O op)
{
    unsigned int iElement = blockDim.x * blockIdx.x + threadIdx.x;
    if (iElement < N)
        C[iElement] = op(A[iElement], B[iElement]);
}
// Host code
int main()
{
    ...
        VectorOperation << <blocks, threads >> >(v1, v2, v3, N, Add());
    ...
}

timg

目录
相关文章
|
并行计算 异构计算
|
并行计算 编译器 缓存
|
存储 并行计算 程序员
|
并行计算 异构计算 安全
|
并行计算 异构计算
|
并行计算 前端开发
|
并行计算 API 异构计算
|
并行计算 程序员 异构计算
|
存储 并行计算
|
并行计算 算法