Skip to content

Instantly share code, notes, and snippets.

@dpiponi
Created December 19, 2011 22:42
Show Gist options
  • Save dpiponi/1499248 to your computer and use it in GitHub Desktop.
Save dpiponi/1499248 to your computer and use it in GitHub Desktop.
Minimal CUDA example
#include <stdio.h>
#define N 1000
__global__
void add(int *a, int *b) {
int i = blockIdx.x;
if (i<N) {
b[i] = 2*a[i];
}
}
int main() {
int ha[N], hb[N];
int *da, *db;
cudaMalloc((void **)&da, N*sizeof(int));
cudaMalloc((void **)&db, N*sizeof(int));
for (int i = 0; i<N; ++i) {
ha[i] = i;
}
cudaMemcpy(da, ha, N*sizeof(int), cudaMemcpyHostToDevice);
add<<<N, 1>>>(da, db);
cudaMemcpy(hb, db, N*sizeof(int), cudaMemcpyDeviceToHost);
for (int i = 0; i<N; ++i) {
printf("%d\n", hb[i]);
}
cudaFree(da);
cudaFree(db);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment