Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "time.h"
- #include "math.h"
- #include "cuda.h"
- #define N 1000000
- __global__ void AddVec(float * x, float * y)
- {
- int i = threadIdx.x;
- float a = 1.2f;
- y[i] = a * x[i] + y[i];
- }
- void uzupelnij(float * x, float * y, int n)
- {
- int i;
- for (i = 0; i < n; ++i)
- {
- x[i] = i;
- y[i] = 2 * i;
- }
- }
- int main(int argc, char const *argv[])
- {
- clock_t czas1, czas2;
- float * x, *y, * dx, * dy;
- uzupelnij(x, y, N);
- cudaMalloc(&dx, N * sizeof(float));
- cudaMalloc(&dy, N * sizeof(float));
- cudaMemcpy(dx, x, N * sizeof(float), cudaMemcpyHostToDevice);
- cudaMemcpy(dy, y, N * sizeof(float), cudaMemcpyHostToDevice);
- czas1 = clock();
- AddVec<<<1, N>>>(x, y);
- czas2 = clock();
- printf("Ilosc czasu to: %f\n", (float)(czas2 - czas1) / CLOCKS_PER_SEC);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment