use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.
the class ContextTest method testVectorNormalize.
@Test
public void testVectorNormalize() throws Exception {
Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
final Float2[] theA = { new Float2(10f, 20f) };
final Float2[] theResult = new Float2[] { new Float2(-1f, -1f) };
try (Context theContext = thePlatform.createContext()) {
theContext.compute(1, new Kernel() {
public void processWorkItem() {
int id = get_global_id(0);
Float2 theVec = VectorFunctions.normalize(theA[id]);
theResult[id].s1 = theVec.s1;
}
});
}
for (Float2 aTheResult : theResult) {
System.out.println(aTheResult);
}
}
use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.
the class AliceBobCarolDaveTest method testSimilarityWithMethodInKernel.
@Test
public void testSimilarityWithMethodInKernel() throws Exception {
// The data of our four friends
Float4 theAlice = new Float4(5f, 1f, 0f, 6f);
Float4 theBob = new Float4(0f, 10f, 3f, 0f);
Float4 theCarol = new Float4(2f, 6f, 3f, 2f);
Float4 theDave = new Float4(7f, 2f, 1f, 8f);
// We need an input for our kernel, a list of vectors
Float4[] theInputs = new Float4[] { theAlice, theCarol, theBob, theDave };
// This is the computed output
int[] theMostSimilar = new int[theInputs.length];
float[] theMostSimilarity = new float[theInputs.length];
// We obtain a platform
Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
// used to cache memory buffers and compiled kernels
try (Context theContext = thePlatform.createContext()) {
// We fire up the computations
theContext.compute(theInputs.length, new Kernel() {
private float similarityOf(Float4 a, Float4 b) {
return dot(a, b) / length(a) * length(b);
}
// This method is called for every workitem
@Override
public void processWorkItem() {
// This is the id of the current work item
int theCurrentWorkItemId = get_global_id(0);
// This is the total number of work items
int theMax = get_global_size(0);
// We obtain the current work item from the list
Float4 theCurrent = theInputs[theCurrentWorkItemId];
float theMaxSimilarity = -1;
int theMaxIndex = -1;
// except itself
for (int i = 0; i < theMax; i++) {
if (i != theCurrentWorkItemId) {
Float4 theOther = theInputs[i];
float theSimilarity = similarityOf(theCurrent, theOther);
if (theSimilarity > theMaxSimilarity) {
theMaxSimilarity = theSimilarity;
theMaxIndex = i;
}
}
}
// The highest similarity is written to the output
theMostSimilar[theCurrentWorkItemId] = theMaxIndex;
theMostSimilarity[theCurrentWorkItemId] = theMaxSimilarity;
}
});
}
// Output the results
for (int i = 0; i < theInputs.length; i++) {
System.out.println("Most similar match for input " + i + " is " + theMostSimilar[i] + " with a similarity of " + theMostSimilarity[i]);
}
}
use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.
the class ContextTest method testSimpleAdd.
@Test
public void testSimpleAdd() throws Exception {
Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
final float[] theA = { 10f, 20f, 30f, 40f };
final float[] theB = { 100f, 200f, 300f, 400f };
final float[] theResult = new float[4];
try (Context theContext = thePlatform.createContext()) {
theContext.compute(4, new Kernel() {
public void processWorkItem() {
int id = get_global_id(0);
float a = theA[id];
float b = theB[id];
theResult[id] = a + b;
}
});
}
for (float aTheResult : theResult) {
System.out.println(aTheResult);
}
}
use of de.mirkosertic.bytecoder.unittest.Slf4JLogger in project Bytecoder by mirkosertic.
the class ContextTest method testComplexAdd.
@Test
public void testComplexAdd() throws Exception {
Platform thePlatform = PlatformFactory.resolve().createPlatform(new Slf4JLogger());
final Float2[] theA = { new Float2(10f, 20f) };
final Float2[] theB = { new Float2(10f, 20f) };
final Float2[] theResult = new Float2[] { new Float2(-1f, -1f) };
try (Context theContext = thePlatform.createContext()) {
theContext.compute(1, new Kernel() {
public void processWorkItem() {
int id = get_global_id(0);
float aS0 = theA[id].s0;
float aS1 = theB[id].s1;
theResult[id].s0 = aS0 + 100;
theResult[id].s1 = aS1 + 200;
}
});
}
for (Float2 aTheResult : theResult) {
System.out.println(aTheResult);
}
}
Aggregations