TEST(test_scenario_te42kyfo, main) { EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); CLArrayFloat *out = cl->arrayFloat(5); CLArrayFloat *in = cl->arrayFloat(5); for(int i = 0; i < 5; i++) { (*out)[i] = 0; } for(int i = 0; i < 100; i++) { for(int n = 0; n < 5; n++) { (*in)[n] = i*n; } kernel->in(in); kernel->out(out); kernel->run_1d(5, 5); assertEquals(i*2 + 5, (*out)[2]); assertEquals(i*4 + 5, (*out)[4]); } cout << "finished" << endl; delete in; delete out; delete kernel; delete cl; }
TEST(testfloatwrapperconst, main) { if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); float in[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; } float out[5]; CLWrapper *inwrapper = cl->wrap(5, (float const *)in); CLWrapper *outwrapper = cl->wrap(5, out); inwrapper->copyToDevice(); kernel->input(inwrapper); kernel->output(outwrapper); kernel->run_1d(5, 5); outwrapper->copyToHost(); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); cout << "tests completed ok" << endl; delete inwrapper; delete outwrapper; delete kernel; delete cl; }
TEST(testinout, main) { if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); float inout[5]; for(int i = 0; i < 5; i++) { inout[i] = i * 3; } kernel->inout(5, inout); size_t global = 5; size_t local = 5; kernel->run(1, &global, &local); assertEquals(inout[0] , 7); assertEquals(inout[1] , 10); assertEquals(inout[2] , 13); assertEquals(inout[3] , 16); assertEquals(inout[4] , 19); cout << "tests completed ok" << endl; delete kernel; delete cl; }
TEST(testdefines, simple) { EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "testDefines", "-D DOUBLE -D SOME_VALUE=5"); float out[32]; kernel->out(32, out); kernel->run_1d(32, 32); EXPECT_EQ(10, out[3]); delete kernel; delete cl; }
TEST(testkernelstore, cl_deletes) { // checks the new deleteWithCl parameter if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernela = cl->buildKernelFromString(getKernel(), "test", ""); CLKernel *kernelb = cl->buildKernelFromString(getKernel(), "test", ""); CLKernel *kernelc = cl->buildKernelFromString(getKernel(), "test", ""); CLKernel *kerneld = cl->buildKernelFromString(getKernel(), "test", ""); cl->storeKernel("kernela", kernela); cl->storeKernel("kernelb", kernelb, true); cl->storeKernel("kernelc", kernelc, false); cl->storeKernel("kerneld", kerneld, true); delete kernela; delete kernelc; delete cl; }
TEST(testqueues, defaultqueue) { cout << "start" << endl; if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); float in[5]; float out[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } kernel->input(5, in); kernel->output(5, out); size_t global = 5; size_t local = 5; kernel->run(1, &global, &local); cl->finish(); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } cout << "cl->queue " << (void *)cl->queue << endl; cout << "*cl->queue " << (void *)*cl->queue << endl; cout << "cl->default_queue->queue" << (void *)cl->default_queue->queue << endl; kernel->input(5, in); kernel->output(5, out); kernel->run(cl->default_queue, 1, &global, &local); cl->finish(); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); cout << "tests completed ok" << endl; }
TEST( testMemset, basic ) { EasyCL *cl = DeepCLGtestGlobals_createEasyCL(); CLKernel *kMemset = 0; // [[[cog // import stringify // stringify.write_kernel2( "kMemset", "cl/memset.cl", "cl_memset", '""' ) // ]]] // generated using cog, from cl/memset.cl: const char * kMemsetSource = "// Copyright Hugh Perkins 2015 hughperkins at gmail\n" "//\n" "// This Source Code Form is subject to the terms of the Mozilla Public License,\n" "// v. 2.0. If a copy of the MPL was not distributed with this file, You can\n" "// obtain one at http://mozilla.org/MPL/2.0/.\n" "\n" "kernel void cl_memset(global float *target, const float value, const int N) {\n" " #define globalId get_global_id(0)\n" " if ((int)globalId < N) {\n" " target[globalId] = value;\n" " }\n" "}\n" "\n" ""; kMemset = cl->buildKernelFromString(kMemsetSource, "cl_memset", "", "cl/memset.cl"); // [[[end]]] int N = 10000; float *myArray = new float[N]; CLWrapper *myArrayWrapper = cl->wrap( N, myArray ); myArrayWrapper->createOnDevice(); kMemset->out( myArrayWrapper )->in( 99.0f )->in( N ); int workgroupSize = 64; kMemset->run_1d( ( N + workgroupSize - 1 ) / workgroupSize * workgroupSize, workgroupSize ); cl->finish(); myArrayWrapper->copyToHost(); for( int i = 0; i < 10; i++ ) { // cout << "myArray[" << i << "]=" << myArray[i] << endl; } for( int i = 0; i < N; i++ ) { EXPECT_EQ( 99.0f, myArray[i] ); } delete kMemset; delete cl; }
TEST(testfloatarray, main) { if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); float in[5]; float inout[5]; float out[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; inout[i] = i * 3; } kernel->in(5, in); kernel->out(5, out); kernel->inout(5, inout); kernel->run_1d(5, 5); for(int i = 0; i < 5; i++) { cout << out[i] << " "; } cout << endl; for(int i = 0; i < 5; i++) { cout << inout[i] << " "; } cout << endl; assertEquals(inout[0], 7); assertEquals(inout[1] , 10); assertEquals(inout[2] , 34); assertEquals(inout[3] , 16); assertEquals(inout[4], 19); assertEquals(out[0] , 5); assertEquals(out[1] , 8); assertEquals(out[2] , 26); assertEquals(out[3] , 14); assertEquals(out[4] , 17); cout << "tests completed ok" << endl; delete kernel; delete cl; }
TEST(testkernelstore, main) { if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); EXPECT_FALSE(cl->kernelExists("kernela")); EXPECT_FALSE(cl->kernelExists("kernelb")); cl->storeKernel("kernela", kernel); EXPECT_TRUE(cl->kernelExists("kernela")); EXPECT_FALSE(cl->kernelExists("kernelb")); EXPECT_EQ(kernel, cl->getKernel("kernela")); delete kernel; delete cl; }
TEST(testqueues, main) { cout << "start" << endl; if(!EasyCL::isOpenCLAvailable()) { cout << "opencl library not found" << endl; exit(-1); } cout << "found opencl library" << endl; EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu(); CLQueue *queue2 = cl->newQueue(); CLQueue *queue3 = cl->newQueue(); CLKernel *kernel = cl->buildKernelFromString(getKernel(), "test", ""); // CLKernel *kernel2 = cl->buildKernelFromString(queue, getKernel(), "test", ""); float in[5]; float out[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } kernel->input(5, in); kernel->output(5, out); size_t global = 5; size_t local = 5; kernel->run(1, &global, &local); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); // in[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } // float out[5]; kernel->input(5, in); kernel->output(5, out); // size_t global = 5; // size_t local = 5; kernel->run(queue2, 1, &global, &local); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); // in[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } // float out[5]; kernel->input(5, in); kernel->output(5, out); // size_t global = 5; // size_t local = 5; kernel->run(queue3, 1, &global, &local); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); // in[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } // float out[5]; kernel->input(5, in); kernel->output(5, out); // size_t global = 5; // size_t local = 5; kernel->run(1, &global, &local); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); // in[5]; for(int i = 0; i < 5; i++) { in[i] = i * 3; out[i] = 0; } // float out[5]; kernel->input(5, in); kernel->output(5, out); // size_t global = 5; // size_t local = 5; kernel->run(queue2, 1, &global, &local); assertEquals(out[0] , 7); assertEquals(out[1] , 10); assertEquals(out[2] , 13); assertEquals(out[3] , 16); assertEquals(out[4] , 19); delete queue2; delete queue3; cout << "tests completed ok" << endl; }