mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-26 19:51:27 +08:00
portable version of mmap
This commit is contained in:
5
go.mod
5
go.mod
@@ -7,3 +7,8 @@ require (
|
|||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/edsrzf/mmap-go v1.1.0 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||||
|
)
|
||||||
|
4
go.sum
4
go.sum
@@ -1,10 +1,14 @@
|
|||||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||||
|
github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ=
|
||||||
|
github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
32
network.go
32
network.go
@@ -9,10 +9,10 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/edsrzf/mmap-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ func (n *YOLONetwork) Init() error {
|
|||||||
|
|
||||||
So, the point of this method is to be able create network configuration via Golang and then pass it to `C.load_network`
|
So, the point of this method is to be able create network configuration via Golang and then pass it to `C.load_network`
|
||||||
|
|
||||||
This code is platform specific. We need to use some portable technique I guess.
|
This code is portable for Windows/Linux/MacOS. See the ref.: https://github.com/edsrzf/mmap-go#mmap-go
|
||||||
*/
|
*/
|
||||||
func (n *YOLONetwork) InitFromDefinedCfg() error {
|
func (n *YOLONetwork) InitFromDefinedCfg() error {
|
||||||
wFile := C.CString(n.WeightsFile)
|
wFile := C.CString(n.WeightsFile)
|
||||||
@@ -80,6 +80,7 @@ func (n *YOLONetwork) InitFromDefinedCfg() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Can't read file bytes")
|
return errors.Wrap(err, "Can't read file bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a temporary file.
|
// Create a temporary file.
|
||||||
tmpFile, err := ioutil.TempFile("", "")
|
tmpFile, err := ioutil.TempFile("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -91,17 +92,27 @@ func (n *YOLONetwork) InitFromDefinedCfg() error {
|
|||||||
return errors.Wrap(err, "Can't write network's configuration into temporary file")
|
return errors.Wrap(err, "Can't write network's configuration into temporary file")
|
||||||
}
|
}
|
||||||
defer tmpFile.Close()
|
defer tmpFile.Close()
|
||||||
|
|
||||||
// Open the temporary file.
|
// Open the temporary file.
|
||||||
fd, err := syscall.Open(tmpFile.Name(), syscall.O_RDWR, 0)
|
// fd, err := syscall.Open(tmpFile.Name(), syscall.O_RDWR, 0)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return errors.Wrap(err, "Can't re-open temporary file")
|
// return errors.Wrap(err, "Can't re-open temporary file")
|
||||||
}
|
// }
|
||||||
defer syscall.Close(fd)
|
// defer syscall.Close(fd)
|
||||||
// Create a memory mapping of the file.
|
// Create a memory mapping of the file.
|
||||||
addr, err := syscall.Mmap(fd, 0, len(cfgBytes), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
// addr, err := syscall.Mmap(fd, 0, len(cfgBytes), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||||||
|
// if err != nil {
|
||||||
|
// return errors.Wrap(err, "Can't mmap on temporary file")
|
||||||
|
// }
|
||||||
|
// Unmap the memory-mapped file.
|
||||||
|
// defer syscall.Munmap(addr)
|
||||||
|
|
||||||
|
// Map the temporary file to memory.
|
||||||
|
mapping, err := mmap.Map(tmpFile, mmap.RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Can't mmap on temporary file")
|
return errors.Wrap(err, "Can't mmap on temporary file")
|
||||||
}
|
}
|
||||||
|
defer mapping.Unmap() // Unmap the memory-mapped file.
|
||||||
|
|
||||||
// GPU device ID must be set before `load_network()` is invoked.
|
// GPU device ID must be set before `load_network()` is invoked.
|
||||||
C.cuda_set_device(C.int(n.GPUDeviceIndex))
|
C.cuda_set_device(C.int(n.GPUDeviceIndex))
|
||||||
@@ -117,10 +128,7 @@ func (n *YOLONetwork) InitFromDefinedCfg() error {
|
|||||||
metadata := C.get_metadata(nCfg)
|
metadata := C.get_metadata(nCfg)
|
||||||
n.Classes = int(metadata.classes)
|
n.Classes = int(metadata.classes)
|
||||||
n.ClassNames = makeClassNames(metadata.names, n.Classes)
|
n.ClassNames = makeClassNames(metadata.names, n.Classes)
|
||||||
// Unmap the memory-mapped file.
|
|
||||||
if err := syscall.Munmap(addr); err != nil {
|
|
||||||
return errors.Wrap(err, "Can't revert mmap")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user