CUDA小记(4)something before programming(一)

本文介绍了CUDA编程中常用的内存初始化函数memset()以及数据传递函数memcpy(),详细阐述了它们在主机与设备间数据操作的作用。
摘要由CSDN通过智能技术生成

我是参考了《GPU高性能CUDA实战》来学习CUDA C编程的,具体分析样例时采用了NVIDIA自带的样例,API有一些不一致的地方我会做简单的说明。
在编程开始之前,我们要对GPU有个简单的理解。GPU,图像处理单元,但我使用CUDA编程最主要的想法是使GPU能把强大的计算能力应用到通用并行计算里。我们将CPU以及系统的内存称为主机,而将GPU以及其内存称为设备。
下面我们仔细分析一下之前的样例,在并行编程之前了解一些常用API和概念。(本节我们仅关注内存的分配、释放和数据传输。)
// includes, system
#include <stdio.h>

// includes CUDA Runtime
#include <cuda_runtime.h>

// includes, project
#include <helper_cuda.h>
#include <helper_functions.h> // helper utility functions

    ...
bool correct_output(int *data, const int n, const int x)
{
    for (int i = 0; i < n; i++)
        if (data[i] != x)
        {
            printf("Error! data[%d] = %d, ref = %d\n", i, data[i], x);
            return false;
        }

    return true;
}

int main(int argc, char *argv[])
{
    int devID;
    cudaDeviceProp deviceProps;///mark

    printf("[%s] - Starting...\n", argv[0]);

    // This will pick the best possible CUDA capable device
    devID = findCudaDevice(argc, (const char **)argv);

    // get device name
    checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
    printf("CUDA device [%s]\n", deviceProps.name);

    int n = 16 * 1024 * 1024;
    int nbytes = n * sizeof(int);
    int value = 26;

    // allocate host memory
    int *a = 0;
    checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
    memset(a, 0, nbytes);

    // allocate device memory
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值