底下以向量相加的範例,示範 UVA 的宣告與使用:
#include<stdio.h> #include<time.h> #include<stdlib.h> #define SIZE 100000 __global__ static void vecAdd(int *a, int *b, int *c, int len){ int idx = blockIdx.x * blockDim.x + threadIdx.x; if(idx<len){ c[idx] = a[idx] + b[idx]; } } int main(int argc,char **argv) { int *a, *b, *c1, *c2; int i, err = 0; cudaHostAlloc((void**)&a, sizeof(int) * SIZE, cudaHostAllocDefault); cudaHostAlloc((void**)&b, sizeof(int) * SIZE, cudaHostAllocDefault); cudaHostAlloc((void**)&c1, sizeof(int) * SIZE, cudaHostAllocDefault); cudaHostAlloc((void**)&c2, sizeof(int) * SIZE, cudaHostAllocDefault); srand(time(NULL)); for(i=0;i<SIZE;i++){ a[i] = rand() % 100; b[i] = rand() % 100; c1[i] = a[i] + b[i]; } vecAdd<<<100, 1000>>>(a, b, c2, SIZE); cudaThreadSynchronize(); for(i=0;i<SIZE;i++){ err += abs(c1[i]-c2[i]); } printf("Err: %d", err); return 0; }