一个nvcc编译的小问题

下面的cuda代码为什么指定compute capability = 8.0 后在A100上跑结果不对?

nvcc a.cu # 正确
nvcc a.cu -gencode arch=compute_80,code=sm_80 # 结果不正确
// a.cu
#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>

using ValueType = float;

__constant__ ValueType col_idx[10];

__global__ void aa_kernel(ValueType *x) {
    int idx = threadIdx.x;
    printf("idx = %d\n", idx);
    if(idx < 10)
        x[idx] = col_idx[idx];
}

int main() {
  ValueType *col_h = new ValueType[10];
  col_h[0] = 1.0;
  for(int i=1; i<10; i++) col_h[i] = 2*col_h[i-1];

  ValueType * x_d;
  cudaError_t err = cudaSuccess;
  err = cudaMalloc(&x_d, 32*sizeof(ValueType));
  if(err != cudaSuccess) printf("error\n");
  cudaMemcpyToSymbol(col_idx, col_h, 10*sizeof(ValueType));
  cudaStream_t stream;
  cudaStreamCreate(&stream);

  aa_kernel<<<1, 32, 0, stream>>>(x_d);
  cudaDeviceSynchronize();

  ValueType x[32] = {0};

  cudaMemcpy(x, x_d, 32*sizeof(ValueType), cudaMemcpyDeviceToHost);
  for(int i=0; i<12; i++) printf("%f\n", x[i]);
  return 0;
}

posted @   zwlwf  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示