第一个CUDA程序

来源:互联网 发布:数控车床椭圆编程实例 编辑:程序博客网 时间:2024/05/21 09:11

第一个CUDA程序

/***  addition: C = A + B.** This sample is a very basic sample that implements element by element* vector addition. It is the same as the sample illustrating Chapter 2* of the programming guide with some additions like error checking.*/#include <stdio.h>#include <iostream>// For the CUDA runtime routines (prefixed with "cuda_")#include <cuda_runtime.h>/*** CUDA Kernel Device code** Computes the vector addition of A and B into C. The 3 vectors have the same* number of elements numElements.*/__global__ void Add(int a, int b, int *c){    *c = a + b;}int main(){    int c;    int *devc;    cudaError_t err = cudaSuccess;    err = cudaMalloc((void **)&devc, sizeof(int));    if (err != cudaSuccess)    {        fprintf(stderr, "Failed to allocate device vector A (error code %s)!\n", cudaGetErrorString(err));        exit(EXIT_FAILURE);    }    Add << <1, 1 >> >(2, 7, devc);    err = cudaMemcpy(&c, devc, sizeof(int), cudaMemcpyDeviceToHost);    if (err != cudaSuccess)    {        fprintf(stderr, "Failed to allocate device vector A (error code %s)!\n", cudaGetErrorString(err));        exit(EXIT_FAILURE);    }    printf("2+7=%d\n", c);    cudaFree(devc);    system("pause");    return 0;}
0 0
原创粉丝点击