# Object-specific allocator for cv::Mat

**URL:** <https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355>\
**Category:** C++\
**Created:** [September 28, 2022, 10:13pm UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355 "2022-09-28T22:13:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fijoy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fijoy/32/882_2.png) [@fijoy](https://forum.opencv.org/u/fijoy)\
**Post date:** [September 28, 2022, 10:13pm UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/1 "2022-09-28T22:13:38Z")

</div>

Does OpenCV support object-specific allocators for cv::Mat class?

I’m using OpenCV’s GPU routines and would like to allocate some of the cv::Mat objects using pinned memory. As per [this link](https://stackoverflow.com/questions/62166540/how-to-allocate-opencv-mat-image-on-cuda-pinned-memory), I can call the cv::Mat::setDefaultAllocator() routine to use pinned memory, but that routine is a static member that will make all Mat objects use pinned memory, correct?

As an alternative to using cv::Mat::setDefaultAllocator(), I looked into setting the public “allocator” attribute of a Mat object. But as per [this Github issue](https://github.com/opencv/opencv/issues/21637), the allocator attribute is not really used.

Is there any other way I can set the allocator for specific Mat objects?

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [September 29, 2022, 6:06am UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/2 "2022-09-29T06:06:35Z")

</div>

> [@fijoy](#):
>
> Is there any other way I can set the allocator for specific Mat objects?

You could allocate and then pin the memory afterwards using [void cv::cuda::registerPageLocked ( Mat& _m_ )](https://docs.opencv.org/4.x/d9/d41/group __cudacore__ struct.html#ga6d25da8194cc95035994ae98e9eebc02) or just use [cv::cuda::HostMem](https://docs.opencv.org/3.4/d0/d44/classcv_1_1cuda_1_1HostMem.html) directly.

---

<div class="post-metadata">

**Author:** ![fijoy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fijoy/32/882_2.png) [@fijoy](https://forum.opencv.org/u/fijoy)\
**Post date:** [October 3, 2022, 9:52pm UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/3 "2022-10-03T21:52:01Z")

</div>

Thanks - using [cv::cuda::registerPageLocked ( Mat& _m_ )](https://docs.opencv.org/4.x/d9/d41/group __cudacore__ struct.html#ga6d25da8194cc95035994ae98e9eebc02) worked! Using pinned memory improved the performance of my application by 47%.

Out of curiosity, I tried cv::Mat::setDefaultAllocator() also, but that had no effect on performance. The exact call I had in my application was:

cv::Mat::setDefaultAllocator(cv::cuda::HostMem::getAllocator(cv::cuda::HostMem::AllocType::PAGE\_LOCKED));

Is there an issue with this OpenCV routine, or am I missing something? The OpenCV version I’m using is 4.5.3.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [October 4, 2022, 5:59am UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/4 "2022-10-04T05:59:54Z")

</div>

> [@fijoy](#):
>
> Thanks - using [cv::cuda::registerPageLocked ( Mat& _m_ )](https://docs.opencv.org/4.x/d9/d41/group __cudacore__ struct.html#ga6d25da8194cc95035994ae98e9eebc02) worked! Using pinned memory improved the performance of my application by 47%.

That’s great glad I could help! I find `cv::cuda::HostMem` is better if you can use it because you don’t need to remember to call `cv::cuda::unregisterPageLocked()` when your done.

> [@fijoy](#):
>
> Out of curiosity, I tried cv::Mat::setDefaultAllocator() also, but that had no effect on performance. The exact call I had in my application was:
> 
> cv::Mat::setDefaultAllocator(cv::cuda::HostMem::getAllocator(cv::cuda::HostMem::AllocType::PAGE\_LOCKED));
> 
> Is there an issue with this OpenCV routine, or am I missing something? The OpenCV version I’m using is 4.5.3.

I wouldn’t have thought there would be an issue with that version of OpenCV. I just confirmed on the master that changing the allocater forced all alocations of `cv::Mat` to be pinned. If you have built a debug version I would check that this line is hit

> <https://github.com/opencv/opencv/blob/7208f6322122f3065d526b0ef19b74c635c8464d/modules/core/src/cuda_host_mem.cpp#L95>

when allocating new `cv::Mat`’s after changing the default allocator.

---

<div class="post-metadata">

**Author:** ![fijoy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fijoy/32/882_2.png) [@fijoy](https://forum.opencv.org/u/fijoy)\
**Post date:** [October 4, 2022, 3:37pm UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/5 "2022-10-04T15:37:38Z")

</div>

Thanks! Confirming that cv::Mat::setDefaultAllocator() not working for me was an error on my part. Resolved now.

One clarification: pinning with cv::cuda::HostMem is useful only when new cv::Mat objects are allocated, correct? And you have to know the size and type of the allocation also? For example, if I want a pinned cv::Mat after a call to cv::imdecode(), I cannot use cv::cuda::HostMem, unless I know in advance the size and type of the image being decoded?

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [October 4, 2022, 4:09pm UTC](https://forum.opencv.org/t/object-specific-allocator-for-cv-mat/10355/6 "2022-10-04T16:09:45Z")

</div>

I would guess from the function signature

```auto
cv::imdecode (InputArray buf, int flags, Mat *dst)

```

that you can’t pass a `HostMem` object as `dst` because it is a `Mat` and not an `OutputArray`. That said I know `VideoCapture::read` works and I would expect all functions which the output is an `OutputArray` to allow you to pass in an empty `HostMem` object and internally alocate it for you.
