/* * test.cl * * Created on: Jan 22, 2012 * Author: scannon */ #ifndef TEST_CL #define TEST_CL //#include "ranluxcl.cl" //#include "Perceptron.cl" char test_PerceptronNode(ranluxcl_state_t *rst) { PerceptronNode pNode1; float16 w1 = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i1 = (float16)(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f); PerceptronNode_init(&pNode1, w1, -7.0f); if(PerceptronNode_calc(&pNode1, i1) >= 0.001f) return 1; PerceptronNode pNode2; float16 w2 = (float16)(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f); float16 i2 = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init(&pNode2, w2, -7.0f); if(PerceptronNode_calc(&pNode2, i2) >= 0.001f) return 2; PerceptronNode pNode3; float16 w3 = (float16)(1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i3 = (float16)(0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init(&pNode3, w3, -2.0f); if(PerceptronNode_calc(&pNode3, i3) - 0.731f > 0.001f || PerceptronNode_calc(&pNode3, i3) - 0.731f < -0.001f) return 3; PerceptronNode pNode4; float16 i4 = (float16)(0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init2(&pNode4, 4); if(PerceptronNode_calc(&pNode4, i4) - 0.731f > 0.001f || PerceptronNode_calc(&pNode4, i4) - 0.731f < -0.001f) return 4; PerceptronNode pNode5; float16 w5 = (float16)(1.0f, 3.0f, 2.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i5 = (float16)(1.0f, 3.0f, 5.0f, 7.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init(&pNode5, w5, -7.0f); if(PerceptronNode_calc(&pNode5, i5) - 0.999f > 0.001f || PerceptronNode_calc(&pNode5, i5) - 0.999f < -0.001f) return 5; PerceptronNode pNode6; PerceptronNode pNode7; PerceptronNode_init3(&pNode6, 7, rst); PerceptronNode_init3(&pNode7, 7, rst); if(PerceptronNode_calc(&pNode6, i5) > 1 || PerceptronNode_calc(&pNode6, i5) < 0) return 6; if(PerceptronNode_calc(&pNode7, i5) > 1 || PerceptronNode_calc(&pNode7, i5) < 0) return 6; if(pNode6.w.s0 == pNode7.w.s0 && pNode6.w.s1 == pNode7.w.s1 && pNode6.w.s2 == pNode7.w.s2 && pNode6.w.s3 == pNode7.w.s3 && pNode6.w.s4 == pNode7.w.s4 && pNode6.w.s5 == pNode7.w.s5 && pNode6.w.s6 == pNode7.w.s6) return 6; PerceptronNode pNode8; float16 i8 = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init2(&pNode8, 0); if(PerceptronNode_calc(&pNode8, i8) - 0.5f > 0.001f || PerceptronNode_calc(&pNode8, i8) - 0.5f < -0.001f) return 7; return 0; } char test_Perceptron(ranluxcl_state_t *rst) { int i = 0; Perceptron p1; float16 i1 = (float16)(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float y1[48]; Perceptron_init2(&p1, 10); Perceptron_calc2(&p1, i1, y1); if(y1[0] - 0.999f > 0.001f || y1[0] - 0.999f < -0.001f) return 11; if(y1[47] - 0.999f > 0.001f || y1[47] - 0.999f < -0.001f) return 11; Perceptron p2; PerceptronNode hiddenNodes[16]; PerceptronNode outputNodes[48]; float16 w2 = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 wh2a = (float16)(11.0f, 11.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 wh2b = (float16)(5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 w02 = (float16)(11.0f, -15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); PerceptronNode_init(&hiddenNodes[0], wh2a, -7.0f); PerceptronNode_init(&hiddenNodes[1], wh2b, -7.0f); for(i = 2; i < 16; i++) PerceptronNode_init(&hiddenNodes[i], w2, -7.0f); PerceptronNode_init(&outputNodes[0], w02, -7.0f); for(i = 1; i < 48; i++) PerceptronNode_init(&outputNodes[i], w2, -7.0f); Perceptron_init(&p2, hiddenNodes, outputNodes); float y[48]; float16 i2a = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i2b = (float16)(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i2c = (float16)(0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float16 i2d = (float16)(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); Perceptron_calc2(&p2, i2a, y); if(y[0] >= 0.15f) return 12; Perceptron_calc2(&p2, i2b, y); if(y[0] <= 0.85f) return 13; Perceptron_calc2(&p2, i2c, y); if(y[0] <= 0.85f) return 14; Perceptron_calc2(&p2, i2d, y); if(y[0] >= 0.15f) return 15; return 0; } char test_GMM(ranluxcl_state_t *rst) { int numSamples = 1000; int i = 0; float mean = 0; float sumX2 = 0; float var = 0; Perceptron p1; Perceptron_init2(&p1, 0); GMM node1; GMM__init(&node1, &p1, 0, 0); for(i = 0; i < numSamples; i++) { float sample = GMM_sample(&node1, rst); mean += sample; sumX2 += sample*sample; node1.sampled = false; } mean /= numSamples; var = sumX2/numSamples - mean*mean; if(mean - 0.5f > 0.05f || mean - 0.5f < -0.05f) return 21; if(var - 0.25f > 0.05f || var - 0.25f < -0.05f) return 22; PerceptronNode hiddenNodes[16]; for(i = 0; i < 16; i++) PerceptronNode_init2(&hiddenNodes[i], 0); PerceptronNode outputNodes[48]; float16 w1 = (float16)(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); float bias[3] = {0.0f, -4.0f, 0.0f}; for(i = 0; i < 48; i++) PerceptronNode_init(&outputNodes[i], w1, bias[i%3]); Perceptron p2; Perceptron_init(&p1, hiddenNodes, outputNodes); Perceptron_init(&p2, hiddenNodes, outputNodes); GMM node2; GMM__init(&node1, &p1, 0, 0); GMM__init(&node2, &p2, 0, 0); Perceptron p3; PerceptronNode hiddenNodes2[16]; for(i = 0; i < 16; i++) PerceptronNode_init2(&hiddenNodes2[i], 2); PerceptronNode outputNodes2[48]; float16 w2 = (float16)(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f); float bias2[3] = {-7.0f, -12.0f, 0.0f}; for(i = 0; i < 48; i++) PerceptronNode_init(&outputNodes2[i], w2, bias2[i%3]); Perceptron_init(&p3, hiddenNodes2, outputNodes2); GMM node3; GMM * parents[16] = {&node1, &node2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; GMM__init(&node3, &p3, parents, 2); mean = 0; sumX2 = 0; for(i = 0; i < numSamples; i++) { float sample = GMM_sample(&node3, rst); mean += sample; sumX2 += sample*sample; node3.sampled = false; } mean /= numSamples; var = sumX2/numSamples - mean*mean; if(mean - 0.73f > 0.05f || mean - 0.73f < -0.05f) return 23; if(var - 0.0003f > 0.0005f || var - 0.0003f < -0.0005f) return 10000*var;//24; return 0; } kernel void Kernel_Ranluxcl_Init(private uint ins, global ranluxcl_state_t *ranluxcltab) { ranluxcl_initialization(ins, ranluxcltab); } __kernel void test_PerceptronNode_kernel(global ranluxcl_state_t *ranluxcltab, __global char *result) { int gid = get_global_id(0); ranluxcl_state_t ranluxclstate; // Download state ranluxcl_download_seed(&ranluxclstate, ranluxcltab); // Conduct tests result[gid] = 0; result[gid] = test_PerceptronNode(&ranluxclstate); // Upload state again ranluxcl_upload_seed(&ranluxclstate, ranluxcltab); } __kernel void test_Perceptron_kernel(global ranluxcl_state_t *ranluxcltab, __global char *result) { int gid = get_global_id(0); ranluxcl_state_t ranluxclstate; // Download state ranluxcl_download_seed(&ranluxclstate, ranluxcltab); // Conduct tests result[gid] = 0; result[gid] = test_Perceptron(&ranluxclstate); // Upload state again ranluxcl_upload_seed(&ranluxclstate, ranluxcltab); } __kernel void test_GMM_kernel(global ranluxcl_state_t *ranluxcltab, __global char *result) { int gid = get_global_id(0); ranluxcl_state_t ranluxclstate; // Download state ranluxcl_download_seed(&ranluxclstate, ranluxcltab); // Conduct tests result[gid] = 0; result[gid] = test_GMM(&ranluxclstate); // Upload state again ranluxcl_upload_seed(&ranluxclstate, ranluxcltab); } #endif // TEST_CL