iwein
iwein

Reputation: 1

Speed problems with memcpy()

I use std::memcpy() to copy an image, given by a high speed camera, to a buffer.

The overall idea is that I copy & buffer camera scanlines into a std::vector; when the capturing is finished, I'll construct a libtorch tensor from this. This tensor is then used further in the software to do image processing.

This works fine: the time needed to copy the contents to the buffer is typically ~1 millisecond;

torch::from_blob((char*)image->buf, { res.spatial, res.spectral }, torch::TensorOptions().dtype(torch::kUInt8)).unsqueeze(0).clone()

...and the result is correct in terms of memory layout.

However: sometimes the copy time is 40-80 milliseconds, which is 40-80 times (!!) more than the expected time.

Why is this? And how do I solve this?

std::vector<torch::Tensor> result;          
result.reserve(nbrOfLines);

camera->startCameraStream();
auto tensor = torch::empty({res.spatial, res.spectral }, torch::kUInt8);
 for (auto lineIndex = 0; lineIndex < nbrOfLines; lineIndex++)
 {
    struct image_buffer* image;
    auto success = camera->getImage(&image); 
    while (!success)                
        success = camera->getImage(&image);             

    result.push_back(torch::from_blob((char*)image->buf, { res.spatial, res.spectral }, torch::TensorOptions().dtype(torch::kUInt8)).unsqueeze(0).clone());

    // Return the result so it can be filled again...
    camera->returnImage(&image);
 }

 auto framegrab = torch::cat(result,0);

We have tried different copying methods, different memory layouts. The code also runs in a HIGH_PRIORITY thread.


EDIT

In the code above, I've replaced memcpy() with torch::from_blob(), but the problems remains.

Upvotes: -3

Views: 129

Answers (0)

Related Questions