CUDA 常數與指針的傳遞

#include
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
__global__ void func3(int * const data, int num_elements);
#define ARRAY_SIZE 10
#define ARRAY_SIZE_IN_BYTES (sizeof(int)*(ARRAY_SIZE))
#define KERNEL_LOOP 10
int cpu_data[ARRAY_SIZE];
int main()
{
const int num_blocks = 2;
const int num_threads = ARRAY_SIZE / num_blocks;
int * gpu_data;
cudaMalloc((void **)&gpu_data, ARRAY_SIZE_IN_BYTES);
func3 <<<num_blocks, num_threads >>>(gpu_data, ARRAY_SIZE);
cudaMemcpy(cpu_data, gpu_data, ARRAY_SIZE_IN_BYTES, cudaMemcpyDeviceToHost);
cudaFree(gpu_data);
for (int i = 0; i < ARRAY_SIZE; i++)
{
cout << cpu_data[i] << "  " <<  endl;
}
cin.get();
return 0;
}
__device__ static int d_tmp[ARRAY_SIZE];
__global__ void func3(int * const data, int num_elements)
{
const int tid = (blockIdx.x*blockDim.x) + threadIdx.x;
if (tid < num_elements)
{
int d_tmp = 0;
for (int i = 0; i < KERNEL_LOOP; i++)
{
d_tmp =1+ d_tmp;
}
data[tid] = d_tmp;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章