Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
steinbac
20150118-cuda-intro
Commits
433001aa
Commit
433001aa
authored
Jan 18, 2016
by
steinbac
Browse files
added version from workshop
parent
aeae8cf7
Changes
1
Hide whitespace changes
Inline
Side-by-side
cuda/vector_sum.cu
0 → 100644
View file @
433001aa
#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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment