Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 7b876de

Browse files
eaplataniosrxwei
authored andcommitted
Addressed comments from swiftlang/swift#25147. (#140)
Simplify `_ExecutionContext.init()` runtime initialization code.
1 parent 835d143 commit 7b876de

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Sources/TensorFlow/Core/Runtime.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ public final class _ExecutionContext {
569569
// Initialize the TF runtime exactly once. Only affects local execution
570570
// (when _RuntimeConfig.tensorFlowServer is set to "").
571571
if !_RuntimeConfig.tensorFlowRuntimeInitialized {
572+
// Install a signal handler to ensure we exit when interrupted.
573+
signal(SIGINT) { _ in
574+
print("Caught interrupt signal, exiting...")
575+
exit(1)
576+
}
577+
572578
var args = ["dummyProgramName"]
573579
if _RuntimeConfig.printsDebugLog {
574580
args.append("--alsologtostderr")
@@ -588,24 +594,19 @@ public final class _ExecutionContext {
588594

589595
// Calculate the addresses of all the strings within our single buffer, and then call
590596
// TF_InitMain.
591-
flattenedStringBytes.withUnsafeMutableBufferPointer { flattenedStringBytesBuffer in
597+
flattenedStringBytes.withUnsafeMutableBufferPointer { buffer in
592598
var stringAddrs: [UnsafeMutablePointer<Int8>?] = []
593-
var currentStringAddr = flattenedStringBytesBuffer.baseAddress
599+
var currentStringAddr = buffer.baseAddress
594600
.map(UnsafeMutablePointer.init)
595601
for length in lengths {
596602
stringAddrs.append(currentStringAddr)
597603
currentStringAddr = currentStringAddr?.advanced(by: length)
598604
}
599605

600606
stringAddrs.withUnsafeMutableBufferPointer { stringAddrsBuffer in
601-
var cArgs = [stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)]
602-
var cArgsCount = [Int32(args.count)]
603-
604-
cArgs.withUnsafeMutableBufferPointer { cArgsBuffer in
605-
cArgsCount.withUnsafeMutableBufferPointer { cArgsCountBuffer in
606-
TF_InitMain(nil, cArgsCountBuffer.baseAddress, cArgsBuffer.baseAddress)
607-
}
608-
}
607+
var cArgsCount = Int32(args.count)
608+
var cArgs = stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)
609+
TF_InitMain(nil, &cArgsCount, &cArgs)
609610
}
610611
}
611612
_RuntimeConfig.tensorFlowRuntimeInitialized = true

0 commit comments

Comments
 (0)