I wrote a simple Cuda C program which takes N numbers and multiplies them by a factor c; since this factor is a constant I decided to put it in the constant memory.
Here is the main.cu code:
#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:
__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?