Skip to content

Programming Guides

This tutorial provides a quick introduction to using Coral API. We will show how to write accelerated applications in Java, Scala, and C++.

Application Examples-

Suppose we wish to write an accelerated application using the Coral API. We will walk through a simple Vector Addition application in Java, Scala and C++, which will target the following OpenCL kernel (accelerator).

vadd.cl

__kernel void vadd(__global float *a, __global float *b, __global float *c, const unsigned int length);

Our program creates three vectors a, b and c of size length. The first two (a, b) are used for the addition, while the result is stored in c. Then a new accelerator request is created and all the required arguments are set one by one in the correct order the accelerator kernel expects them. For the buffers an additional argument is given (R, W or RW) which informs Coral of the expected operation.

Access Flags

  • Read (R): Accelerator will use this memory only for reading.

  • Write (W): Accelerator will use this memory only for writing.

  • Read-Write (RW): Accelerator will use this memory both for reading and writing.

If access flag is left blank the memory is automatically marked as Read-Write.

import java.util.Random;

import com.inaccel.coral.InAccel;
import com.inaccel.coral.shm.SharedFloatVector;

public class VectorAddition{

    public static int main(String[] args) throws Exception{
        int length = 10000;

        // Allocate the three vectors
        SharedFloatVector a =
            new SharedFloatVector(length).alloc();
        SharedFloatVector b =
            new SharedFloatVector(length).alloc();
        SharedFloatVector c =
            new SharedFloatVector(length).alloc();

        Random random = new Random();

        // Generate random values for vectors A, B
        for (int i = 0; i < length; i++){
            // Generate random float value [0.0 - 5.0] for A
            float value = 5 * random.nextFloat();
            a.put(i, value);

            // Generate random float value [0.0 - 5.0] for B
            value = 5 * random.nextFloat();
            b.put(i, value);
        }

        // Send a synchronous request for "Vector Addition" to Coral FPGA Resource Manager
        // Request arguments must comply with the accelerator's specific argument list
        InAccel request = new InAccel("vadd")
                                .arg(a.R()) // Read argument
                                .arg(b.R()) // Read argument
                                .arg(c.W()) // Write argument
                                .arg(length)
                                .run();

        // Print vector C
        for (int i = 0; i < length; i++){
            System.out.printf("%f ", c.get(i));
        }
        System.out.println();

        // Free the three vectors
        a.free();
        b.free();
        c.free();

        return 0;
    }

}
import scala.util.Random

import com.inaccel.coral.InAccel
import com.inaccel.coral.shm.SharedFloatVector

object VectorAddition{

    def main(args: Array[String]){
        val length = 10000;

        // Allocate the three vectors
        val a = new SharedFloatVector(length).alloc
        val b = new SharedFloatVector(length).alloc
        val c = new SharedFloatVector(length).alloc

        val random = new Random()

        // Generate random values for vectors A, B
        for (var i <- 0 until length){
            // Generate random float value [0.0 - 5.0] for A
            var value = 5 * random.nextFloat
            a.put(i, value);

            // Generate random float value [0.0 - 5.0] for B
            value = 5 * random.nextFloat
            b.put(i, value);
        }

        // Send a synchronous request for "Vector Addition" to Coral FPGA Resource Manager
        // Request arguments must comply with the accelerator's specific argument list
        val request = new InAccel("vadd")
                                .arg(a.R) // Read argument
                                .arg(b.R) // Read argument
                                .arg(c.W) // Write argument
                                .arg(length)
                                .run

        // Print vector C
        for (var i <- 0 until length){
            printf("%f ", c.get(i))
        }
        println

        // Free the three vectors
        a.free
        b.free
        c.free
    }

}
#include <cstdlib>

#include <coral/InAccel.h>
#include <coral/shm/SharedFloatVector.h>

int main(){
    int length = 10000;

    // Allocate the three vectors
    SharedFloatVector *a =
        (new SharedFloatVector(length))->alloc();
    SharedFloatVector *b =
        (new SharedFloatVector(length))->alloc();
    SharedFloatVector *c =
        (new SharedFloatVector(length))->alloc();

    // Generate random values for vectors A, B
    for (int i = 0; i < length; i++){
        // Generate random float value [0.0 - 5.0] for A
        float value = (float) rand() / (float) (RAND_MAX / 5);
        a->put(i, value);

        // Generate random float value [0.0 - 5.0] for B
        value = (float) rand() / (float) (RAND_MAX / 5);
        b->put(i, value);
    }

    // Send a synchronous request for "Vector Addition" to Coral FPGA Resource Manager
    // Request arguments must comply with the accelerator's specific argument list
    InAccel *request = (new InAccel("vadd"))
                            ->arg(a->R()) // Read argument
                            ->arg(b->R()) // Read argument
                            ->arg(c->W()) // Write argument
                            ->arg(length)
                            ->run();

    // Print vector C
    for (int i = 0; i < length; i++){
        printf("%f ", c->get(i));
    }
    printf("\n");

    // Free the three vectors
    a->free();
    b->free();
    c->free();

    // Destruct C++ objects
    delete a;
    delete b;
    delete c;

    delete request;

    return 0;
}