Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #define N 1024
- using namespace std;
- __global__ void vectorAdd(int *a, int *b, int *c) {
- int i = blockIdx.x;
- c[i] = a[i] + b[i];
- }
- // create random numbers and put on the vector
- void randomInts(int *vector) {
- int i = 0;
- time_t t;
- srand((unsigned) time(&t));
- for (; i < N; i++)
- vector[i] = rand() % 1000 + 1;
- }
- int main() {
- int *a, *b, *c, i = 0;
- int *d_a, *d_b, *d_c;
- int size = N * sizeof(int);
- a = (int *) malloc(size); randomInts(a);
- b = (int *) malloc(size); randomInts(b);
- c = (int *) malloc(size);
- cudaMalloc(&d_a, size);
- cudaMalloc(&d_b, size);
- cudaMalloc(&d_c, size);
- cudaMemcpy(d_a, a, size, cudaMemcpyHostToDevice);
- cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice);
- vectorAdd<<<N,1>>>(d_a, d_b, d_c);
- cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost);
- // printing the first ten number of result vector
- for (; i < 10; ++i)
- cout << "c[" << i << "] = " << c[i] << endl;
- free(a); free(b); free(c);
- cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement