I hope to use the GPU on a mobile phone to directly and quickly access and synchronize memory data, which the CPU will also read and modify in real-time.
On a mobile phone, this is a simple example, where the printed result is “1 7” instead of “7 7”. It’s known that active synchronization can be achieved using methods like enqueueReadBuffer or enqueueNDRangeKernel, but is there a way to achieve automatic synchronization? Ideally, the GPU should be able to read the real-time updated values of data directly after modification.
data = (int*)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
data[0] = 1;
cl::Buffer inputBuffer = cl::Buffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR , sizeof(int) * dataSize, data, &err);
cl::Buffer outputBuffer = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * dataSize, NULL, &err);
cl::Program::Sources source(1, std::make_pair(kernelSource2, strlen(kernelSource2) + 1));
cl::Program program(context, source);
err = program.build(devices);
cl::Kernel kernel(program, "square", &err);
kernel.setArg(0, inputBuffer);
kernel.setArg(1, outputBuffer);
data[0] = 7;
size_t globalSize = dataSize;
cl::Event event;
err = queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(globalSize), cl::NullRange, nullptr, &event);
int *result = (int*)malloc(sizeof(int) * dataSize);
queue.enqueueReadBuffer(outputBuffer, CL_TRUE, 0, sizeof(int) * dataSize, result);
printf("%d %dn", result[0], data[0])
user28921138 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1