<div dir="ltr">Hi all,<div><br></div><div>Thanks for the thoughts.</div><div><br></div><div>From an algorithmic perspective, it's frustrating to have to wait to read/write data. There are ways, up to a point, to avoid some of this with the current interface, but it can make applications complicated and for algorithms that can process blocks separately, having to wait on reads/writes is unfortunate. I understand that some drivers will be constrained due to the provided APIs, but I don't know that this needs to hold back a more general implementation.</div><div><br></div><div>My thinking at this time is that some other interface is preferable to trying to making the existing code thread-safe. It might make it easier to write threaded applications as well. The current band model that allows mixed reads and writes seems like it would complicate things. I wonder if there are many applications that both read from and write to the same raster.</div><div><br></div><div>GDAL is a long-lived system and it does many, many things well and provides lots of options. Perhaps stripping away a certain amount of the current flexibility would allow much of the driver code to be reused and progress to be made in a more timely manner. The code doesn't have to do everything to be useful.</div><div><br></div><div>Anyway, if you have further ideas, feel free to write me.</div><div><br></div><div>Thanks,</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 5, 2024 at 4:31 PM Deyan Vasilev <<a href="mailto:didomeister@gmail.com">didomeister@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
I've written a piece of software that fetches tiles out of a single<br>
MBTiles raster done by multiple threads. Tiles go to a common cache<br>
which can be used by a "view" thread that assembles tiles on the<br>
screen in a seamless map.<br>
<br>
Each fetcher thread uses GDALOpenEx() and then RasterIO(GF_Read,,,) to<br>
get individual RGB bands out of the stored PNG tiles in the source<br>
MBTiles db. On the lowest level<br>
GDALGPKGMBTilesLikePseudoDataset::ReadTile() does a sqlite3 "SELECT<br>
tile_data FROM tiles..." with a db context which is unique to every<br>
fetcher thread.<br>
<br>
Giving a recap, a multithreaded raster read on a single raster source<br>
is doable, eased probably by the sqlite raster container that I use in<br>
my app.<br>
<br>
Best regards,<br>
Deyan<br>
<br>
<br>
<br>
<br>
On Mon, Jun 3, 2024 at 8:04 PM Even Rouault via gdal-dev<br>
<<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a>> wrote:<br>
><br>
> Andrew,<br>
><br>
> what would be the purpose of thread-safe access: just making it thread-safe without any particular requirement on how efficient this would be (1), or hope for true concurrent access with ideally close to linear scalability with the number of threads (2) ?<br>
><br>
> If (1), then we could add a GDALMutexedDataset class, similarly to <a href="https://github.com/OSGeo/gdal/blob/master/ogr/ogrsf_frmts/generic/ogrmutexeddatasource.h" rel="noreferrer" target="_blank">https://github.com/OSGeo/gdal/blob/master/ogr/ogrsf_frmts/generic/ogrmutexeddatasource.h</a> which exists on the vector side (just used by the FileGDB driver due to the fact that the underlying SDK is not even re-entrant), which uses the decorator pattern around all public API entry points to call the underlying dataset under a mutex.  One could imagine to have a GDAL_OF_THREADSAFE open flag that GDALOpen() would use to return such instance. Shouldn't be too hard to implement, but probably not that useful IMHO. I can anticipate most users would have higher expectations than a mutex-based implementation.<br>
><br>
> If (2), it seems to me that it would require a huge effort, and the programming language we use (C++) offers hardly any safety belt to make sure we don't make mistakes, the main one being forgetting to lock things that should be locked, or dead locks situation. If we go into doing that, I'm not even sure how we can reliably identify all parts of the code that must be modified<br>
><br>
> Neither GDAL raster core nor any driver are designed to be thread-safe. For core, at least gcore/gdalarraybandblockcache.cpp and gcore/gdalhashsetbandblockcache.cpp which interact with the block cache should be made thread-safe, and "just" adding a lock would defeat the aim to achieve linear scalability. The change in GDALDataset::RasterIO() I did in <a href="https://github.com/OSGeo/gdal/commit/7f3a0e582eb189744bc7cb8e4a751135edaecaf5" rel="noreferrer" target="_blank">https://github.com/OSGeo/gdal/commit/7f3a0e582eb189744bc7cb8e4a751135edaecaf5</a> isn't thread-safe either (would be easy to make thread-safe though)<br>
><br>
> Once GDAL raster code is ready, the main challenge is making drivers themselves thread-safe. Raster drivers may directly read from a VSILFILE* handle, which isn't thread safe when using the standard Seek() + Read() pair. A few VSIVirtualFileSystem have a PRead() implementation, which is thread-safe, but not all). Or they rely on using some instance of a "reader" returned by a third-party library (libtiff, libjpeg, libpng, sqlite3, etc.) (which in most cases also uses a VSILFILE*), none of which are thread-safe (except sqlite3 that can be made thread-safe by passing a flag at sqlite3_open() time, that will basically applies strategy (1) by protecting all calls with a mutex). Perhaps using thread-specific instances of VSILFILE* and third-party "reader" objects could be a way of solving this. But realistically doing a pass in all GDAL drivers would be a multi-month-man to multi-year-man type of effort. A realistic plan should be designed to allow combining (1) and (2): (2) for a few select drivers, and (1) as a fallback for most drivers that wouldn't be updated.<br>
><br>
> Even<br>
><br>
> Le 03/06/2024 à 15:44, Andrew Bell via gdal-dev a écrit :<br>
><br>
> Hi,<br>
><br>
> I am aware that there isn't thread-safe raster access with the current GDAL interface for various reasons. Given the state of processors, I was wondering if it would be valuable to take a look at providing the ability to do Raster I/O (at least reads) in a thread-safe way. This could be done through a new set of API calls or perhaps by modifications to what currently exists -- I don't know what makes sense at this point. I would be happy to spend some time looking at this if there is interest, but I would also like to learn from existing experience as to what kinds of things that I'm surely not considering would have to be dealt with.<br>
><br>
> Thanks,<br>
><br>
> --<br>
> Andrew Bell<br>
> <a href="mailto:andrew.bell.ia@gmail.com" target="_blank">andrew.bell.ia@gmail.com</a><br>
><br>
> _______________________________________________<br>
> gdal-dev mailing list<br>
> <a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
><br>
> --<br>
> <a href="http://www.spatialys.com" rel="noreferrer" target="_blank">http://www.spatialys.com</a><br>
> My software is free, but my time generally not.<br>
><br>
> _______________________________________________<br>
> gdal-dev mailing list<br>
> <a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Andrew Bell<br><a href="mailto:andrew.bell.ia@gmail.com" target="_blank">andrew.bell.ia@gmail.com</a></div>