#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<stdlib.h>
#define MAX_VALUES 1024
template<class T>
class myValues{
T values[MAX_VALUES];
public:
__device__ myValues(T clear){}
__device__ void setValue(int Idx,T value){
values[Idx] = value;
}
__device__ void putToMemory(int Idx,T* valueLocation){
if(Idx<MAX_VALUES){
valueLocation[Idx] = values[Idx]+Idx;
}
}
};
template<class T>
void __global__ useValues(T* memBuffer){
myValues<T> myLocation(0);
int tid = blockIdx.x*blockDim.x+threadIdx.x;
myLocation.setValue(tid,1);
myLocation.putToMemory(tid,memBuffer);
}
static int* data_host=(int*)malloc(MAX_VALUES*sizeof(int));
__device__ void* buffer;
int main(){
cudaMalloc((void**)&buffer,MAX_VALUES*sizeof(int));
useValues<int><<<2,512>>>((int*)buffer);
cudaMemcpy(data_host,buffer,MAX_VALUES*sizeof(int),cudaMemcpyDeviceToHost);
for(int i=0;i<MAX_VALUES;i++){
printf("%d\n",data_host[i]);
}
system("pause");
return 0;
}
cuda 类模板以及全局内存的使用例子
最新推荐文章于 2021-09-16 17:53:17 发布