#include #include "Common.h" #include "PrimitiveTests.h" #include Square::Square() { const float vmax = 1000.0f; data_size = 4096; std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution distr(0.0f, vmax); for (size_t index = 0; index < data_size; ++index) { float val = distr(gen); gpuResult.push_back(val); sourceData.push_back(val); } } void Square::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) { cl_int err = CL_SUCCESS; // Get the kernel handle cl::Kernel kernel(*program, "square", &err); CheckCLError(err); clInputBuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float) * data_size, NULL, &err); queue->enqueueWriteBuffer(clInputBuffer, true, // Blocking! 0, sizeof(float) * data_size, gpuResult.data()); // Allocate the output data clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * data_size, NULL, &err); // Set the kernel parameters kernel.setArg(0, clInputBuffer); // kernel FV paraméterei sorrendben kernel.setArg(1, clResultBuffer); // Enqueue the kernel queue->enqueueNDRangeKernel(kernel, cl::NullRange, // Indexek nem eloffszetelve cl::NDRange(data_size, 1), // Minden elemet egy szál cl::NullRange, // Workgroup méret? - ez az auto, ha nem indul, 1024-re, onnan csökkent, amig elindul NULL, // Event); // Ő jlezi hogy vége, lsd lent } void Square::cpu_compute() { for (size_t index = 0; index < data_size; index++) { cpuResult.push_back(sourceData[index] * sourceData[index]); } } void Square::collect_results(cl::CommandQueue* queue) { queue->enqueueReadBuffer(clResultBuffer, true, 0, sizeof(float) * data_size, gpuResult.data()); } bool Square::validate_results() { for (size_t index = 0; index < data_size; index++) { if (cpuResult[index] != gpuResult[index]) { std::cout << "Wrong result at [" << index << "]: " << gpuResult[index] << "!=" << cpuResult[index] << std::endl; return false; } } return true; } std::string Square::description() { return std::string("Square (data_size = " + std::to_string(data_size) + ")" ); }