/********************************************************************** Copyright ©2012 Advanced Micro Devices, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ********************************************************************/ #define _VARIADIC_MAX 10 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS #define __CL_ENABLE_EXCEPTIONS #include #include #include #include #include #include #include #include const int NUM_ELEMENTS = 64; /** * Write random number * @param vector stores the random sequence * @param width and height the size of arrayPtr * @param the range of the element * @param seed */ int fillRandom( std::vector &vec, const int width, const int height, const float rangeMin, const float rangeMax, unsigned int seed = 123); /** * Print the vector * @param header * @param data the vector */ void printVector( std::string header, const std::vector vec); /** * Read a file to string * @param filename the file name * @param s stores the file data */ int convertToString( const char *filename, std::string& s); int main(int argc, char * argv[]) { cl_int status = 0; // create a CL program using the kernel source const char *filename = "CplusplusWrapper_Kernels.cl"; std::string sourceStr; status = convertToString(filename, sourceStr); if (status != CL_SUCCESS) { std::cout << "Failed to open " << filename << std::endl; return status; } // create program through .cl file cl::Program vectorAddProgram(std::string(sourceStr), false); try { vectorAddProgram.build(""); } catch(cl::Error e) { std::cout << e.what() << std::endl; std::cout << "Build Status: " << vectorAddProgram.getBuildInfo(cl::Device::getDefault()) << std::endl; std::cout << "Build Options:\t" << vectorAddProgram.getBuildInfo(cl::Device::getDefault()) << std::endl; std::cout << "Build Log:\t " << vectorAddProgram.getBuildInfo(cl::Device::getDefault()) << std::endl; return EXIT_FAILURE; } typedef cl::make_kernel< cl::Buffer&, cl::Buffer&, cl::Buffer& > KernelType; // create kernel as a functor KernelType vectorAddKernel(vectorAddProgram, "vectorAdd" ); // create host memory inputA. std::vector inputA(NUM_ELEMENTS); fillRandom(inputA, NUM_ELEMENTS, 1, 0, 255); printVector("inputA:", inputA); // create host memory inputB. std::vector inputB(NUM_ELEMENTS); fillRandom(inputB, NUM_ELEMENTS, 1, 0, 255, 100); printVector("inputB:", inputB); // create host memory output. std::vector output(NUM_ELEMENTS, 0); // create memory objects. bool isReadOnly = true; cl::Buffer inputABuffer(inputA.begin(), inputA.end(), isReadOnly); cl::Buffer inputBBuffer(inputB.begin(), inputB.end(), isReadOnly); cl::Buffer outputBuffer(output.begin(), output.end(), !isReadOnly); cl::Event e; cl::Platform platform = cl::Platform::getDefault(); if(strcmp(platform.getInfo().c_str(), "Advanced Micro Devices, Inc.")) { std::cout<<"Default platform should be Advanced Micro Devices, Inc. to run this sample\n"< vec) { std::cout<<"\n"<::size_type ix = 0; ix != vec.size(); ++ix) { std::cout< &vec, const int width, const int height, const float rangeMin, const float rangeMax, unsigned int seed) { if(vec.empty()) { std::cout << "Cannot fill vector." << std::endl; return EXIT_FAILURE; } // set seed if(!seed) seed = (unsigned int)time(NULL); srand(seed); // set the range double range = double(rangeMax - rangeMin) + 1.0; /* random initialisation of input */ for(int i = 0; i < height; i++) for(int j = 0; j < width; j++) { int index = i*width + j; vec[index] = rangeMin + float(range*rand()/(RAND_MAX + 1.0)); } return CL_SUCCESS; }