What methods are available to enable zero-copy support on smartphones with Qualcomm GPUs?
I have tried various methods. First, I confirmed the presence of Unified Memory through CL_DEVICE_HOST_UNIFIED_MEMORY and CL_DEVICE_GLOBAL_MEM_CACHE_TYPE. However, after testing the following example, I found that there is no automatic synchronization unless I actively call enqueueReadBuffer to update.The result is “Zero Copy Supported: No” This is the incomprehensible part. Shouldn’t the GPU on mobile phones all support zero-copy?
#include <CL/cl.hpp>
#include <vector>
#include <iostream>
int main() {
const int dataSize = 1024;
std::vector<int> data(dataSize, 1);
cl::Platform platform;
cl::Device device;
cl::Context context;
cl::CommandQueue queue;
cl::Buffer buffer;
try {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
platform = platforms[0];
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
device = devices[0];
context = cl::Context({device});
queue = cl::CommandQueue(context, device);
buffer = cl::Buffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(int) * dataSize, data.data());
const char* kernelSource = R"CLC(
__kernel void modify_data(__global int* data) {
int id = get_global_id(0);
data[id] *= 2;
}
)CLC";
cl::Program::Sources sources;
sources.push_back({kernelSource, strlen(kernelSource)});
cl::Program program(context, sources);
program.build({device});
cl::Kernel kernel(program, "modify_data");
kernel.setArg(0, buffer);
queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(dataSize), cl::NullRange);
queue.finish();
bool zeroCopy = true;
for (int i = 0; i < dataSize; ++i) {
if (data[i] != 2) {
zeroCopy = false;
break;
}
}
std::cout << "Zero Copy Supported: " << (zeroCopy ? "Yes" : "No") << std::endl;
} catch (cl::Error& err) {
std::cerr << "OpenCL Error: " << err.what() << " (" << err.err() << ")" << std::endl;
return -1;
}
return 0;
}
user28921138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.