简体   繁体   English

在随机地址之间快速复制

[英]Fast copying between random addresses

I'm developing an application which needs to perform a massive copying data byte-by-byte from one addresses to another addresses.我正在开发一个应用程序,它需要从一个地址到另一个地址逐字节地执行大量复制数据。 Now'm using for loop in multithread.现在在多线程中使用 for 循环。 Size of arrays can be from 100k elements to 2M elements. arrays 的大小可以从 100k 个元素到 2M 个元素。 It works relatively fast, but not enough.它的工作速度相对较快,但还不够。 Is there a faster way to perform this task?有没有更快的方法来执行这个任务?

std::vector<uchar*> src, dst
//Filling src and dst vectors with pointers. src.size() and dst.size() are equal.

for (int i=0; i<src.size();i++)
   *dst[i]=*src[i]

UPD: It's an image processing task where pixel is 8-bit grayscale. UPD:这是一个像素为 8 位灰度的图像处理任务。 Ready-to-use solutions such as OpenCV isn't suitable because it's even slower (up to 20 times). OpenCV 等即用型解决方案不适合,因为它甚至更慢(最多 20 倍)。 Maybe GPU solution is possible?也许 GPU 解决方案是可能的?

I'm developing an application which needs to perform a massive copying data byte-by-byte我正在开发一个需要逐字节执行大量复制数据的应用程序

That's very unlikely.这是非常不可能的。

The only reason to create a copy is that the data is being modified in some way (and different pieces of code can't just share the same data in a "read only" way);创建副本的唯一原因是数据正在以某种方式被修改(不同的代码不能只是以“只读”方式共享相同的数据); and if the data is being modified in some way then it's very likely that you can merge the modification into the copying.如果正在以某种方式修改数据,那么您很可能可以将修改合并到复制中。

Maybe you're doing the same changes to all pixels, and it can be (eg) a "read 16 pixels from source, modify 16 pixels, write 16 pixels to destination" loop (where the work involved in modifying the pixels happens in parallel with pre-fetching the next pixels into cache, etc).也许您正在对所有像素进行相同的更改,它可以是(例如)“从源读取 16 个像素,修改 16 个像素,将 16 个像素写入目标”循环(其中涉及修改像素的工作并行发生将下一个像素预取到缓存中,等等)。

Maybe you're only modifying some pixels, and can do (eg) a lazy if( pointer_to_row[row] == NULL) { pointer_to_row[row] = create_copy_of_row(row); } modify_row(pointer_to_rows[row]);也许您只是在修改一些像素,并且可以(例如)做一个懒惰的if( pointer_to_row[row] == NULL) { pointer_to_row[row] = create_copy_of_row(row); } modify_row(pointer_to_rows[row]); if( pointer_to_row[row] == NULL) { pointer_to_row[row] = create_copy_of_row(row); } modify_row(pointer_to_rows[row]); to avoid copying all the rows of pixels you don't modify.以避免复制您未修改的所有像素行。 Maybe you can create a shared memory mapping of the data and let the operating system's "copy on write" virtual memory management take care of the copying for you.也许您可以创建一个共享的 memory 数据映射,让操作系统的“写时复制”虚拟 memory 管理为您处理复制。

Maybe you can have some kind of journal of changes and leave the original data alone (where you might have an int get_pixel(int x, int y ) { int temp = check_journal(x, y); if(temp;= NOT_PRESENT) return temp, else return get_original_pixel_data(x; y); } .也许您可以拥有某种更改日志并单独保留原始数据(您可能有一个int get_pixel(int x, int y ) { int temp = check_journal(x, y); if(temp;= NOT_PRESENT) return temp, else return get_original_pixel_data(x; y); }

Maybe you can combine multiple techniques (eg a small journal for each row of pixels, with a lazy "if/when journal for row becomes full, create new row from old row and journal, and reset the journal to empty").也许你可以结合多种技术(例如,每行像素的小日志,懒惰的“如果/当行日志变满时,从旧行和日志创建新行,并将日志重置为空”)。

I moved entire project on GPU, using GLSL.我使用 GLSL 在 GPU 上移动了整个项目。 Arrays are replaced with 2D samplers. Arrays 替换为 2D 采样器。 Even low-end Intel UHD can handle high resolutions at high framerate.即使是低端英特尔 UHD 也可以在高帧率下处理高分辨率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM