起因
由於要將剔除最小連通域中 std:;vetor 這個數據容器,在 kernel 核函數也有類似的使用,所以,查閱資料等百度,完後就找到了 thrust:: 中的 __host_vector, 和 device_vector 這兩個東西。然而發現,其依舊需要在本地代碼段中來 提前擴展其大小,而不能在kernel 中使用…不過就當記錄一下吧,爲了以後可以直接拿來使用
代碼
//先加載需要 的頭文件
//添加cuda數組.
#include <thrust/reduce.h>
#include <thrust/sequence.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
核函數
__global__ void nestedHelloWorld(int const iSize, int iDepth, int2* vecLists, int vecListSize)
{
int tid = threadIdx.x;
printf("Recursion=%d: Hello World from thread %d block %d\n", iDepth, tid, blockIdx.x);
if (iSize == 1) return;
int nthreads = iSize >> 1;
if (tid == 0 && nthreads > 0)
{
printf("-------> nested execution depth: %d\n", iDepth);
}
for (int i = 0; i < vecListSize; i++)
{
int2 temp;
temp.x = i;
temp.y = i * 10;
vecLists[i] = temp;
// vecLists[i];
//printf("threadIdx: %d int2: %d %d\n",threadIdx.x, temp.x, temp.y);
}
}
本地函數:
void IteratorMyselfTest()
{
thrust::device_vector<int2> vecLists;
vecLists.resize(200);
//這裏需要轉爲一級指針.,最後將參數傳進去.
int2* vecListPtr = thrust::raw_pointer_cast(&vecLists[0]);
int vecListSize = vecLists.size();
nestedHelloWorld << <1, 10 >> > (iszie, idepth, vecListPtr, vecListSize);
//打印
for (int i = 0; i < vecListSize; i++)
{
int2 temp = vecLists[i];
printf("idx: %d int2: %d %d\n", i, temp.x, temp.y);
}
}
以上就是簡單的使用方式. 額外插一句,上面那個 thrust 這個庫,可以直接使用其內置的函數 做 加減乘除,甚至可以你自己重寫 openrate()(…),完成自定義計算方式.