简体   繁体   English

我是否正确使用常量内存?

[英]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.

相关问题 我是否正确使用结构? - Am I using structs correctly? 如何在 C(使用我正在使用的 DS)中为此 MergeSort 实现正确分配 memory? - How can I correctly allocate memory for this MergeSort implementation in C (with the DS I am using)? C语言编程:SIGABRT 134错误; 我是否可以正确释放此内存? - C programming: SIGABRT 134 error; am I freeing this memory correctly? 我是否正确使用ctypes来pythonify这个结构? - Am I using ctypes correctly to pythonify this struct? 我是否正确使用了mutex_trylock? - Am I using the mutex_trylock correctly? 我需要知道我是否正确使用了getline - I need to know if I am using getline correctly 需要知道如何在 c 中按空格解析单词。 还需要知道我是否正确分配内存? - Need to know how to parse words by space in c. Also need to know if I am allocating memory correctly? 添加的值将无法正确保存到malloc数组。 我是否忽略了内存泄漏? - Added values wont save to malloc array correctly. Is there a memory leak I am overlooking? 我是否正确使用数组? 如果是这样,为什么我会收到核心转储错误? - Am I using arrays correctly? If so, why am I getting a core dump error? 我使用多少堆栈内存? <C> - How Much Stack Memory am I Using? <C>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM