-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cu
More file actions
45 lines (38 loc) · 1.06 KB
/
vector.cu
File metadata and controls
45 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cuda_runtime_api.h>
#include <device_launch_parameters.h>
#include <tiny_helper_cuda.h>
#include <vector.cuh>
__global__ void
vectormult_kernel(float *A, float k, int numElements)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < numElements)
{
A[i] *= k;
}
}
__global__ void
vectoradd_kernel(const float* A, const float* B, float* C, int numElements)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < numElements)
{
C[i] = A[i] + B[i];
}
}
void vectormult(float* dev_vector, float k, int size)
{
const int n_threads = 128;
const int n_blocks = (size + n_threads - 1) / n_threads;
vectormult_kernel <<<n_blocks, n_threads >>>(dev_vector, k, size);
checkCudaErrors(cudaDeviceSynchronize());
getLastCudaError("vectormult_kernel");
}
void vectoradd(const float* A, const float* B, float* C, int size)
{
const int n_threads = 128;
const int n_blocks = (size + n_threads - 1) / n_threads;
vectoradd_kernel <<<n_blocks, n_threads >>>(A, B, C, size);
checkCudaErrors(cudaDeviceSynchronize());
getLastCudaError("vectormult_kernel");
}