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
6b17df4f
Commit
6b17df4f
authored
Jan 18, 2016
by
steinbac
Browse files
added VectorSum
parent
9eb2f7e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
cuda-java/VectorSum.java
0 → 100644
View file @
6b17df4f
//package com.sun.jna.examples;
import
com.sun.jna.Library
;
import
com.sun.jna.Native
;
import
com.sun.jna.Platform
;
/** Simple example of JNA interface mapping and usage. */
public
class
VectorSum
{
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public
interface
CLibrary
extends
Library
{
CLibrary
INSTANCE
=
(
CLibrary
)
Native
.
loadLibrary
((
Platform
.
isWindows
()
?
"msvcrt"
:
"c"
),
CLibrary
.
class
);
void
native_vector_sum
(
Pointer
host_a
,
Pointer
host_b
,
unsigned
n_elements
);
}
public
static
void
main
(
String
[]
args
)
{
CLibrary
cuda_lib
=
(
CLibrary
)
Native
.
loadLibrary
(
"libvectorsum"
,
CLibrary
.
class
);
// Memory allocates a contiguous block of memory suitable for sharing with C directly - no copying is necessary
Pointer
vec_a
=
new
Memory
(
100
*
Native
.
getNativeSize
(
Float
.
TYPE
));
Pointer
vec_b
=
new
Memory
(
100
*
Native
.
getNativeSize
(
Float
.
TYPE
));
// note: Memory instance (and its associated memory allocation) will be freed when ex8p goes out of scope
for
(
int
dloop
=
0
;
dloop
<
100
;
dloop
++)
{
// populate the array with junk data (just for the sake of the example)
vec_a
.
setFloat
(
dloop
*
Native
.
getNativeSize
(
Float
.
TYPE
),
11
);
vec_b
.
setFloat
(
dloop
*
Native
.
getNativeSize
(
Float
.
TYPE
),
2
);
}
// call the C function
double
add
=
clib
.
native_vector_sum
(
vec_a
,
vec_b
,
100
);
//check the result
Double
sum
=
0
;
for
(
int
dloop
=
0
;
dloop
<
100
;
dloop
++)
{
sum
+=
vec_a
[
dloop
];
}
System
.
out
.
println
(
"sum of 100*13 = "
+
sum
);
}
}
\ 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