Commit 6b17df4f authored by steinbac's avatar steinbac
Browse files

added VectorSum

parent 9eb2f7e6
//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"),
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
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