[英]Am I using constant memory correctly?
I wrote a simple Cuda C program which takes N numbers and multiplies them by a factor c ; 我编写了一个简单的Cuda C程序,该程序将N个数字乘以系数c ; since this factor is a constant I decided to put it in the constant memory. 因为这个因素是一个常数,所以我决定将其放入常数存储器中。
Here is the main.cu code : 这是main.cu代码 :
#include <stdlib.h>
#include <stdio.h>
__constant__ float c; // IS THIS OK?
typedef struct{
float a;
}variable;
variable *var_CPU,*var_GPU;
#include "kernel.cu"
//==============================================================
int main(void){
int i,N=100;
var_CPU = (variable*)malloc(N*sizeof(variable));
for(i=0;i<N;i++){
var_CPU[i].a=(float)i;
printf("var_CPU[%d].a=%f\n",i,var_CPU[i].a);
}
float pc=2; // IS THIS OK?
cudaMemcpyToSymbol(c, &pc, sizeof(c)); // IS THIS OK?
cudaMalloc((void**)&var_GPU,N*sizeof(variable));
cudaMemcpy(var_GPU,var_CPU,N*sizeof(variable), cudaMemcpyHostToDevice);
CollisioniGPU<<<10, 10>>>(var_GPU);
cudaGetLastError();
cudaMemcpy(var_CPU, var_GPU, N*sizeof(variable),cudaMemcpyDeviceToHost);
for(i=0;i<N;i++){
printf("var_CPU[%d].a=%f\n",i,var_CPU[i].a);
}
cudaFree(var_GPU);
free(var_CPU);
return 0;
}
Here is the kernel.cu : 这是kernel.cu :
__device__ void funzione(float *var){
*var = *var*c;
}
__global__ void CollisioniGPU(variable *var_GPU){
int id= blockIdx.x*blockDim.x + threadIdx.x;
float a;
a=var_GPU[id].a;
funzione(&a);
var_GPU[id].a = a;
}
Here is my question : Is this a correct way of using the constant memory ? 这是我的问题 :这是使用常量内存的正确方法吗? How can I be sure that the multiplication factor c is saved there and not somewhere else? 如何确定乘法因子c保存在此处而不是其他位置?
It seems correct to me, through I would have written things a bit differently. 对我来说似乎是正确的,因为我写的东西会有所不同。
For definition of c, i would put it in kernel.cu and add extern keyword in main.cu. 对于c的定义,我将其放在kernel.cu中,并在main.cu中添加extern关键字。 (And because I am lazy, I often just put these two files in one with two parts divided by some comment block.) (并且由于我很懒,所以我经常只是将这两个文件放在一起,然后将其分为两部分,再加上一些注释块。)
For line with float pc = 2; 对于浮点pc = 2的行; I often add point and zero, because it's easier to distinguish between integer and floating point variables while reading the code. 我经常加点和零,因为在读取代码时更容易区分整数变量和浮点变量。
And for the cudaMemcpyToSymbol(c, &pc, sizeof(c)); 对于cudaMemcpyToSymbol(c,&pc,sizeof(c)); I would have used sizeof(float). 我会用过sizeof(float)。
This all is just matter of prefference and clarification of code. 这一切都只是代码的优选和澄清的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.