Commit 433001aa authored by steinbac's avatar steinbac
Browse files

added version from workshop

parent aeae8cf7
#include <iostream>
#include <vector>
__global__ void sum_two_vectors(float* _device_a,
float* _device_b,
unsigned int _size){
unsigned int index = blockIdx.x*blockDim.x + threadIdx.x;
if(index < _size)
_device_a[index] = _device_a[index] + _device_b[index];
}
int main(int argc, char** argv){
unsigned int vector_size = 1000*argc;
std::vector<float> host_a(vector_size,12.);
std::vector<float> host_b(vector_size,30.);
std::cout << "created vectors of size " << vector_size << std::endl;
unsigned int vector_size_byte = vector_size*sizeof(float);
float* device_a = nullptr;
float* device_b = nullptr;
cudaMalloc(&device_a,vector_size_byte);
cudaMalloc(&device_b,vector_size_byte);
cudaMemcpy(device_a,&host_a[0],vector_size_byte,cudaMemcpyHostToDevice);
cudaMemcpy(device_b,&host_b[0],vector_size_byte,cudaMemcpyHostToDevice);
//work to be done on device_a and device_b
unsigned int threads = 32;//dim3
unsigned int blocks = (vector_size + threads -1 )/threads;
sum_two_vectors<<<blocks,threads>>>(device_a, device_b, vector_size);
cudaMemcpy(&host_a[0],device_a,vector_size_byte,cudaMemcpyDeviceToHost);
if(host_a[0]!=12.f)
std::cout << "GPU altered host_a\n";
cudaFree(device_a);
cudaFree(device_b);
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment